Skip to content

Instantly share code, notes, and snippets.

@abhishek2x
Created November 29, 2020 10:17
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/26b3d670827ba59ab9cc4c9bab077030 to your computer and use it in GitHub Desktop.
Save abhishek2x/26b3d670827ba59ab9cc4c9bab077030 to your computer and use it in GitHub Desktop.
Finding Floor of an Element in a Sorted array
/*!
* Finding Floor 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 = 13;
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];
l = mid+1;
}
else {
h = 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