Skip to content

Instantly share code, notes, and snippets.

@abhishek2x

abhishek2x/2d.cpp

Created Dec 19, 2020
Embed
What would you like to do?
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