Skip to content

Commit

Permalink
search element in 2D array
Browse files Browse the repository at this point in the history
  • Loading branch information
Raushan-Kumar007 committed Aug 29, 2022
1 parent 87b5958 commit 861ff67
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions matrix/SearchElmIn2Dmat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package matrix;

public class SearchElmIn2Dmat {
// Naive approch
// static void search(int mat[][], int x){
// int R = mat.length;
// int C = mat.length;
// for(int i=0;i<R;i++){
// for(int j=0;j<C;j++){
// if(mat[i][j]==x){
// System.out.println("found at C"+i+","+j);
// return;
// }
// }
// }
// System.out.println("NotFound");
// }
// efficient Approch
static void search(int mat[][], int x){
int R = mat.length;
int C = mat.length;
int i=0, j=C-1;
while(i<R && j>=0){
if(mat[i][j]==x){
System.out.println("found at C"+i+","+j);
return;
}else if(mat[i][j]>x)
j--;
else
i++;
}
System.out.println("Not Found");
}
public static void main(String[] args) {
int arr[][] = {{12,13,14,15},{20,21,22,23},{32,33,34,35},{44,45,46,47}};
search(arr, 21);
}
}

0 comments on commit 861ff67

Please sign in to comment.