-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix-search.cpp
64 lines (44 loc) · 1.09 KB
/
matrix-search.cpp
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
63
int binarySearch(vector<int > &arr, int x)
{
int mid;
int low = 0;
int high = arr.size()-1;
while(low<=high)
{
mid = (low+high)/2;
if(x == arr[mid])
return mid;
else if(x > arr[mid])
low = mid+1;
else
high = mid-1;
}
return -1;
}
int Solution::searchMatrix(vector<vector<int> > &arr, int x) {
int m = arr.size();
int n = arr[0].size();
bool flag = false;
int result;
for(int i=0; i<m; i++)
{
result = binarySearch(arr[i],x);
//cout<<result<<" ";
if(result != -1)
{
flag = true;
break;
}
}
if(flag == true)
{
// cout<<1<<endl;
return 1;
}
else
{
// cout<<0<<endl;
return 0;
}
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
}