We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 136e6c0 commit 62eef08Copy full SHA for 62eef08
search/Staircase Search in a 2D Matrix
@@ -0,0 +1,35 @@
1
+#include <bits/stdc++.h>
2
+using namespace std;
3
+
4
+bool staircaseSearch(vector<vector<int>>& matrix, int target) {
5
+ int rows = matrix.size();
6
+ int cols = matrix[0].size();
7
+ int row = 0, col = cols - 1;
8
9
+ while (row < rows && col >= 0) {
10
+ if (matrix[row][col] == target) {
11
+ return true;
12
+ } else if (matrix[row][col] > target) {
13
+ col--;
14
+ } else {
15
+ row++;
16
+ }
17
18
+ return false;
19
+}
20
21
+int main() {
22
+ vector<vector<int>> matrix = {
23
+ {1, 4, 7, 11},
24
+ {2, 5, 8, 12},
25
+ {3, 6, 9, 16},
26
+ {10, 13, 14, 17}
27
+ };
28
+ int target = 8;
29
+ if (staircaseSearch(matrix, target))
30
+ cout << "Element found." << endl;
31
+ else
32
+ cout << "Element not found." << endl;
33
34
+ return 0;
35
0 commit comments