Skip to content

Instantly share code, notes, and snippets.

@abhishek2x

abhishek2x/Ceil.cpp

Created Nov 29, 2020
Embed
What would you like to do?
Finding Ceil of an Element in a Sorted array
/*!
* 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