Skip to content

Instantly share code, notes, and snippets.

@abhishek2x
Last active November 29, 2020 10:07
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/e8069cef3e481e7cd5b9283ba4763742 to your computer and use it in GitHub Desktop.
Save abhishek2x/e8069cef3e481e7cd5b9283ba4763742 to your computer and use it in GitHub Desktop.
Binary Search algorithm on a reversed sorted array.
// Binary Search algorithm on a reversed sorted array.
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){
ios :: sync_with_stdio(false);
cin.tie(0);
vector<int> v = {9, 7, 5, 3 , 2};
int l, r, mid, x=7;
l = 0;
r = v.size()-1;
while(l<=r){
mid = l + (r-l)/2;
if(v[mid] == x){
cout << x << " found at " << mid << "\n";
break;
} else if(v[mid] > x){
l = mid + 1;
} else {
r = mid - 1;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment