-
Notifications
You must be signed in to change notification settings - Fork 368
/
binary_search_2d.java
37 lines (33 loc) · 1010 Bytes
/
binary_search_2d.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
public class binary_search_2d{
public static int[] search(int[][] matrix, int target){
int row = 0;
int col = matrix.length - 1;
while(row < matrix.length && col >= 0){
if(matrix[row][col] == target){
return new int[]{row, col};
}
if(matrix[row][col] < target){
row++;
}
else{
col--;
}
}
//if not found in the matrix, return [-1, -1]
return new int[]{-1, -1};
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int [][] matrix= {
{10, 20, 30, 40},
{15, 25, 35, 45},
{28, 29, 37, 49},
{33, 34, 38, 50}
};
System.out.print("Enter the number to be searched in the 2D Matrix : ");
int n = sc.nextInt();
System.out.println(Arrays.toString(search(matrix, n)));
sc.close();
}
}