forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixSearch.java
More file actions
62 lines (46 loc) · 1.16 KB
/
MatrixSearch.java
File metadata and controls
62 lines (46 loc) · 1.16 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
package BinarySearch;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Author - archit.s
* Date - 02/10/18
* Time - 11:32 PM
*/
public class MatrixSearch {
// O(r*(logc))
/*public int searchMatrix(ArrayList<ArrayList<Integer>> a, int b) {
int r = a.size();
int c = a.get(0).size();
for(int i=0;i<r;i++){
if(a.get(i).get(0) <= b && a.get(i).get(c-1) >= b){
if(Arrays.binarySearch(a.get(i).toArray(),b) >= 0){
return 1;
}
}
}
return 0;
}*/
public int searchMatrix(ArrayList<ArrayList<Integer>> a, int b) {
int r = a.size();
int c = a.get(0).size();
int start = 0;
int end = r*c - 1;
int x,y;
int mid;
while(start<=end){
mid = start + (end - start)/2;
x = mid/c;
y = mid%c;
if(a.get(x).get(y) == b ){
return 1;
}
else if(a.get(x).get(y) > b ){
end = mid-1;
}
else{
start = mid + 1;
}
}
return 0;
}
}