Skip to content

Instantly share code, notes, and snippets.

@abhishek2x
Created December 19, 2020 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhishek2x/eb767806ead5ce4516e134c0ffa9a30e to your computer and use it in GitHub Desktop.
Save abhishek2x/eb767806ead5ce4516e134c0ffa9a30e to your computer and use it in GitHub Desktop.
Searching in 2-D Array
/*!
* 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