diff --git a/Medium/Search a 2D Matrix II.java b/Medium/Search a 2D Matrix II.java new file mode 100644 index 0000000..ff26178 --- /dev/null +++ b/Medium/Search a 2D Matrix II.java @@ -0,0 +1,22 @@ +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int row = 0;//Initially we are at integer 1 as per example 1 + int col = matrix[0].length-1;//at element 15 as per example 1 + while(row=0){ + if(target==matrix[row][col]){ + return true; + } + if(targettarget so decrease column by 1 and check, here if we decrease by 1 we will be on 11 + col--; + continue; + } + if(target>matrix[row][col]){ + //moving forward we will end up at 4 which is lesser than target and + //as the matrix is sorted we won't get the element in that row so we will switch the row + row++; + } + } + return false; //if the target element is not present + } +} \ No newline at end of file