diff --git a/search/Staircase Search in a 2D Matrix b/search/Staircase Search in a 2D Matrix new file mode 100644 index 00000000000..1b1193caad1 --- /dev/null +++ b/search/Staircase Search in a 2D Matrix @@ -0,0 +1,35 @@ +#include +using namespace std; + +bool staircaseSearch(vector>& matrix, int target) { + int rows = matrix.size(); + int cols = matrix[0].size(); + int row = 0, col = cols - 1; + + while (row < rows && col >= 0) { + if (matrix[row][col] == target) { + return true; + } else if (matrix[row][col] > target) { + col--; + } else { + row++; + } + } + return false; +} + +int main() { + vector> matrix = { + {1, 4, 7, 11}, + {2, 5, 8, 12}, + {3, 6, 9, 16}, + {10, 13, 14, 17} + }; + int target = 8; + if (staircaseSearch(matrix, target)) + cout << "Element found." << endl; + else + cout << "Element not found." << endl; + + return 0; +}