-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchingAcendingMatrix.cpp
More file actions
53 lines (42 loc) · 1.25 KB
/
searchingAcendingMatrix.cpp
File metadata and controls
53 lines (42 loc) · 1.25 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
#include <iostream>
using namespace std;
int main() {
int rows, columns, key;
cout << "Enter the rows of the array : ";
cin >> rows;
cout << "Enter the columns of the array : ";
cin >> columns;
int arr[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << "Enter the element no. [" << (i + 1) << ',' << (j + 1) << "] : ";
cin >> arr[i][j];
}
}
cout << "Enter the key : ";
cin >> key;
bool found = false;
for (int i = 0; i < rows; i++) {
if (arr[i][0] <= key && (i == rows - 1 || arr[i + 1][0] > key)) {
int left = 0, right = columns - 1, mid = 0;
while (left <= right) {
mid = left + (right - left) / 2;
if (arr[i][mid] == key) {
cout << "At index : [" << i << ',' << mid << ']' << endl;
found = true;
break;
}
else if (arr[i][mid] > key) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
}
}
if (!found) {
cout << "Key not found" << endl;
}
return 0;
}