forked from k-samarth/Data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchElementInMatrixLeetCode.cpp
More file actions
55 lines (49 loc) · 1.26 KB
/
Copy pathSearchElementInMatrixLeetCode.cpp
File metadata and controls
55 lines (49 loc) · 1.26 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
// Optimal Solution for LeetCode find element in a matrix.
//Here the first element of next row is larger than the last element of the first row
#include <bits/stdc++.h>
using namespace std;
void GfGSearchMatrix(vector<vector<int>>&mat,int target){
if(mat.size()==0)
return;
int n= mat.size();
int m= mat[0].size();
int quantity = m*n - 1;
int low=0;
int mid;
int high=quantity;
while(low<=high){
mid = (low+high)/2;
if(mat[mid/n][mid%n]==target){
cout<<"The Element is found at index i="<<mid/n<<" and j="<<mid%n<<endl;
return;
}
else if(mat[mid/n][mid%n] < target){
low = mid+1;
}
else{
high = mid-1;
}
}
cout<<"The Element is not found!"<<endl;
}
int main()
{
int n,m,temp;
cout<<"Enter the no rows"<<endl;
cin>>n;
cout<<"Enter the no of Columns"<<endl;
cin>>m;
vector<vector<int>>mat(n);
cout<<"Please enter the elements in the matrix"<<endl;;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>temp;
mat[i].push_back(temp);
}
}
cout<<"Please enter the target"<<endl;
int target;
cin>>target;
GfGSearchMatrix(mat,target);
return 0;
}