Created
December 19, 2020 14:03
-
-
Save abhishek2x/2435d80884fd22f7aeb7c1e43346cbc1 to your computer and use it in GitHub Desktop.
Searching in Bitonic Infinite 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<int> bInf = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; | |
int l=0, h=1, mid; | |
while(bInf[h] < 1){ | |
h *= 2; | |
} | |
int firstOccr; | |
while(l <= h) { | |
mid = l+(h-l)/2; | |
if(bInf[mid] == 1){ | |
firstOccr = mid; | |
h = mid-1; | |
} | |
else if (bInf[mid] < 1){ | |
l = mid + 1; | |
} else { | |
h = mid-1; | |
} | |
} | |
cout << firstOccr; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment