Created
November 29, 2020 10:17
-
-
Save abhishek2x/18bf48e2c4f52b12c63d863b50e895e8 to your computer and use it in GitHub Desktop.
Finding Ceil of an Element in a Sorted 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
/*! | |
* Finding Ceil of an Element in a 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> arr = {1, 2, 3, 4, 7, 10, 14}; | |
int f = 5; | |
int res; | |
int l=0, h=arr.size()-1, mid; | |
while(l <= h){ | |
mid = l + (h-l)/2; | |
if(f <= arr[mid]){ | |
res = arr[mid]; | |
h = mid-1; | |
} | |
else { | |
l = mid+1; | |
} | |
} | |
cout << res << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment