From e6590ddaa08ac309920e869aa6d9445e8e637f31 Mon Sep 17 00:00:00 2001 From: Chinmay Lohani <56592002+Golden-Hunter@users.noreply.github.com> Date: Sat, 23 Oct 2021 09:50:30 +0530 Subject: [PATCH] Create Staircase_search.cpp --- Staircase_search.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Staircase_search.cpp diff --git a/Staircase_search.cpp b/Staircase_search.cpp new file mode 100644 index 00000000..0487904f --- /dev/null +++ b/Staircase_search.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; +int staircaseSearch(int arr[][100], int n, int key) +{ + int i = 0; + int j = n - 1; + while (i <= n && j >= 0) + { + if (arr[i][j] == key) + { + cout << "Number found at (" << i+1 << "," << j+1 << ")\n"; + return 1; + } + else if (arr[i][j] < key) + { + i++; + } + else + { + j--; + } + } + cout << "Number not found\n"; +} +int main() +{ + int n; + cin >> n; + int arr[100][100] = {0}; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> arr[i][j]; + } + } + int key; + cin >> key; + staircaseSearch(arr, n, key); + return 0; +}