-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixSearch.cpp
More file actions
56 lines (47 loc) · 1.24 KB
/
matrixSearch.cpp
File metadata and controls
56 lines (47 loc) · 1.24 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
#include<bits/stdc++.h>
using namespace std;
int main() {
int row, column, element;
cin >> row >> column >> element;
int a[row][column];
for (int i=0;i<row;i++){
for (int j=0;j<column;j++){
cin >> a[i][j];
}
}
// searching in matrix
bool flag = false;
int r=0, c=column-1; // initializing where to start
while(r < column && c >= 0){
if (a[r][c] == element){
flag = true;
}
a[r][c] > element ? c-- : r++;
}
if (flag){
cout << "Element found!";
}else{
cout << "Element not found!";
}
return 0;
}
// bool searchMatrix(vector<vector<int>>& matrix, int target) {
// if(matrix.size() == 0){
// return false;
// }
// int row = matrix.size();
// int coloumn = matrix[0].size();
// int r = 0;
// int c = coloumn-1;
// while(r >= 0 && c >= 0 && c < coloumn && r < row){
// if(matrix[r][c] == target){
// return true;
// }
// if(matrix[r][c] > target){
// c--;
// }else{
// r++;
// }
// }
// return false;
// }