Skip to content

Commit db829c7

Browse files
committed
Arrays interview question
1 parent d0998ef commit db829c7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class searchInSortedMatrix {
2+
public static void search(int[][]matrix,int n,int x){
3+
int i=0;
4+
int j=n-1;
5+
while (i<n &&j>=0){
6+
if(matrix[i][j]==x){
7+
System.out.println("Found at: "+i+","+j+" (i,j)");
8+
return;
9+
}
10+
if (matrix[i][j]>x){
11+
j--;
12+
}
13+
else {
14+
i++;
15+
}
16+
}
17+
System.out.println("Not found");
18+
}
19+
20+
public static void main(String[] args) {
21+
int [][]matrix={
22+
{1,2,3},
23+
{4,5,6},
24+
{7,8,9}
25+
};
26+
search(matrix,3,4);
27+
}
28+
}

0 commit comments

Comments
 (0)