Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to efficiently find a target element in a sorted matrix of distinct ints, in Java?


/*
	This is an implementation that demonstrates
	how to efficiently find a target element
	within a sorted matrix of distinct ints.
	Each row of the matrix is sorted. Moreover, 
	each column is sorted 
	
	Let n be the number of rows and m be the number
	of columns.

	Time complexity: O(n+m) 
	Space complexity: O(1)
*/
import java.util.Arrays;

public class SortedMatrixSearch {

	public static void main(String[] args) {
		int[][] matrix = {
				{ 1, 4, 7, 12, 15 },
				{ 2, 5, 19, 31, 32 },
				{ 3, 8, 24, 33, 35 },
				{ 40, 41, 42, 44, 45 },
				{ 99, 100, 103, 106, 128 }
		};
		int target = 44;
		// Below outputs: [3, 3] idxes at which 44 is found
		System.out.println(
				Arrays.toString(findInSortedMatrix(matrix, target)));
		target = 1000;
		// Below outputs: [-1, -1] as 1000 is not part of matrix
		System.out.println(
				Arrays.toString(findInSortedMatrix(matrix, target)));
	}

	private static int[] findInSortedMatrix(int[][] matrix, int target) {
		final int ROWS = matrix.length, COLS = matrix[0].length;
		int rowIdx = 0;
		int colIdx = COLS - 1;

		while (rowIdx < ROWS && colIdx >= 0) {
			if (matrix[rowIdx][colIdx] > target) {
				// Go to previous column
				colIdx--;
			} else if (matrix[rowIdx][colIdx] < target) {
				// Go to next row
				rowIdx++;
			} else {
				// Return row and column idxes at which target is found
				return new int[] { rowIdx, colIdx };
			}
		}
		// Failed to find target element
		return new int[] { -1, -1 };
	}

}
Comment

PREVIOUS NEXT
Code Example
Java :: method to check parameters in java 
Java :: java convert array to another type 
Java :: como printar o valor de um campo em um jtextfield 
Java :: system vs integration testing 
Java :: java initialize map with values in one line 
Java :: how to create a list in java 
Java :: youTubeInitializationResult gives SERVICE_MISSING error in android 
Java :: Java program to find the sum of all even numbers from 1 to 10 
Java :: change color of text in textview android 
Java :: How to efficiently find the middle node of a singly linked list, in Java? 
Java :: linear layout background color 
Java :: nums.add to add the number to the array in java sample 
Java :: button color xml 
Java :: how to collect objective in java 
Java :: import for Collectors java 
Java :: java stop executing my program 
Java :: concurrent modification exception 
Java :: print colored text java 
Java :: primefaces datepicker validation 
Java :: onclicklistener in android studio 
Java :: react native android gif 
Java :: char variable java 
Java :: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.kodakjhelum.ItemFragment: make sure class name exists 
Java :: find duplicate value in array using java 
Java :: object to array java 
Java :: sort array java 
Java :: how to check if a char is a letter java 
Java :: password encryption and decryption in java 
Java :: java save file 
Java :: spring boot intellij auto reload 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =