From 15f734289af00d20bfca176c02bc3cc691c5bf58 Mon Sep 17 00:00:00 2001 From: Ashad <93534298+Ashad001@users.noreply.github.com> Date: Fri, 5 Aug 2022 16:49:31 +0500 Subject: [PATCH] Delete BinarySearchIn2DArrays.cpp --- .../Searching/BinarySearchIn2DArrays.cpp | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 algorithms/CPlusPlus/Searching/BinarySearchIn2DArrays.cpp diff --git a/algorithms/CPlusPlus/Searching/BinarySearchIn2DArrays.cpp b/algorithms/CPlusPlus/Searching/BinarySearchIn2DArrays.cpp deleted file mode 100644 index fd473320..00000000 --- a/algorithms/CPlusPlus/Searching/BinarySearchIn2DArrays.cpp +++ /dev/null @@ -1,63 +0,0 @@ -// C++ program to implement Binary Search in 2D Arrays -#include -#include -using namespace std; - -// Floor of a array is the greatest number which is smaller than or equal to the target element -// Search the floor number in the first column. Since the array is sorted that means the target element -// should lie on the row which has the first element as its floor -int Floor(vector> arr, int target) -{ - int start = 0; - int end = arr.size() - 1; - if(target < arr[start][0]) - return arr[start][0]; - while (start <= end) - { - int mid = start + (end - start) / 2; - if(target < arr[mid][0]) - end = mid - 1; - else if(target > arr[mid][0]) - start = mid + 1; - else - return mid; // if it is the target element return the column index - } - return end; // Since the condition in which the loop breaks is start > end that means the end will be smaller than target element - // whereas start will be greater than target element. -} - -// Search just in a row which has its first element smaller than or equal to target element(floor) -vector BinarySearch(vector> arr, int target) -{ - int floor = Floor(arr, target); - int start = 0; - int end = arr[floor].size() - 1; - while(start <= end) - { - int mid = start + (end - start) / 2; - if(target > arr[floor][mid]) - start = mid + 1; - else if(target < arr[floor][mid]) - end = mid - 1; - else - return {floor, mid}; - } - return {-1 , -1}; // If element is not found return negatives. -} - - -int main(int argc, char const *argv[]) -{ - vector> arr = { - {1,2,3,4}, - {5,6,7,8}, - {9,10,11,12}, - {13,14,15,16} - }; - vector ans = BinarySearch(arr, 15); - - cout << "[ " << ans[0] << " , " << ans[1] << " ]" << endl; // [3, 2] for such target element - - - return 0; -} \ No newline at end of file