Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

target element in a sorted matrix of distinct ints


/*
	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 :: how to change orientation of linearlayout in recyclerview android 
Java :: bloomreach remove property 
Java :: java convert double to string 
Java :: android run background service on startup 
Java :: scanner in java 
Java :: java create set with values 
Java :: How to find the power of a number efficiently in Java? 
Java :: rdd. map with condition 
Java :: go to activity android 
Java :: afficher texte java 
Java :: how to build a java main menu 
Java :: spring data rest partial update 
Java :: jlabel on the center of a jpanel 
Java :: checkindex java 
Java :: How to perform an iterative depth first search through a binary tree, in Java? 
Java :: java get unix timestamp 
Java :: java sha256 hex digest 
Java :: java 11 initialize map 
Java :: add value with n variable with Arraylist in java 
Java :: topological sort java 
Java :: how to get length of integer in java 
Java :: how to import java.util 
Java :: hashmap.foreach example 
Java :: java word count 
Java :: java int to hex 
Java :: how to instantiate hashmap in java 
Java :: return day from date in java 
Java :: java string swap two characters 
Java :: open camera in android 
Java :: java display two dimensional array 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =