-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearching.java
More file actions
62 lines (59 loc) · 1.92 KB
/
searching.java
File metadata and controls
62 lines (59 loc) · 1.92 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.ArrayList;
public class searching {
public static ArrayList<Integer> sortedMatrix(int[][] arr,int key)
{
ArrayList<Integer> list = new ArrayList<>();
int diff = Math.min(key - arr[0][arr.length-1],key - arr[arr.length-1][0]);
if(key - arr[0][arr.length-1] == diff)
{
int row=0;
int col =arr[0].length -1;
while(row<=arr.length -1 && col >=0)
{
if(arr[row][col] > key)
{
col--;
}
else if(arr[row][col]<key)
{
row++;
}
else{
list.add(row);
list.add(col);
return list;
}
}
}
else{
int row=arr.length-1;
int col =0;
while(row<=arr.length -1 && col >=0)
{
if(arr[row][col] > key)
{
row--;
}
else if(arr[row][col]<key)
{
col++;
}
else{
list.add(row);
list.add(col);
return list;
}
}
}
System.out.println("Not found");
return list;
}
public static void main(String args[])
{
int[][] arr={{10,20,30,40},{15,25,35,45},{27,29,37,48},{32,33,39,50}};
int key =10;
ArrayList<Integer> list = new ArrayList<>();
list =sortedMatrix(arr,key);
System.out.println(list.get(0)+" "+list.get(1));
}
}