Created
December 19, 2020 14:08
-
-
Save abhishek2x/eb767806ead5ce4516e134c0ffa9a30e to your computer and use it in GitHub Desktop.
Searching in 2-D Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* Copyright (c) 2020 Abhishek Srivastava | |
*/ | |
#include <bits/stdc++.h> | |
using namespace std; | |
#define ll long long int | |
int main(){ | |
ios :: sync_with_stdio(false); | |
cin.tie(0); | |
vector<vector <int> > a = { | |
{10, 20, 30, 40}, | |
{15, 25, 35, 96}, | |
{17, 29, 37, 98}, | |
{32, 33, 39, 50} | |
}; | |
int n = 4; | |
int key = 17; | |
int l=0, h=n-1, mid; | |
while(l<n && h<n && h>=0 &&l>=0){ | |
// cout << l << " -- " << h << "\n"; | |
mid = l + (h-l)/2; | |
if(a[l][h] == key){ | |
cout << l << " " << h; | |
break; | |
} else if(a[l][h] > key){ | |
h -=1; | |
} else{ | |
l+=1; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment