|
| 1 | +package AlgoExpert_Medium; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +public class SearchInASortedMatrix { |
| 6 | + |
| 7 | + public static void main(String[] args) { |
| 8 | + |
| 9 | + int[][] matrix = { { 1, 4, 7, 12, 15, 1000 }, |
| 10 | + { 2, 5, 19, 31, 1001 } , |
| 11 | + { 3, 8, 24, 33, 35, 1002 } , |
| 12 | + { 40, 41, 42, 44, 45, 1003 } , |
| 13 | + { 99, 100, 103, 106, 128, 1004 } |
| 14 | + } ; |
| 15 | + |
| 16 | + int target = 44; |
| 17 | + |
| 18 | + System.out.println(searchInSortedMatrix(matrix, target)); |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + public static int[] searchInSortedMatrix(int[][] matrix, int target) { |
| 23 | + |
| 24 | + int[] default1 = {-1, -1} ; |
| 25 | + |
| 26 | + for(int i=0 ; i<matrix.length; i++ ){ |
| 27 | + |
| 28 | + int index = Arrays.binarySearch(matrix[i], target); |
| 29 | + |
| 30 | + if(index>0 && index <matrix[0].length) |
| 31 | + return new int[] { i, index } ; |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + return default1 ; |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | +} |
0 commit comments