Created
November 29, 2020 10:17
-
-
Save abhishek2x/26b3d670827ba59ab9cc4c9bab077030 to your computer and use it in GitHub Desktop.
Finding Floor 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 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