Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///Finding lowerbound by two method
- #include<bits/stdc++.h>
- using namespace std;
- /*int l_bound(int arr[],int n,int k)
- {
- int lo=0,hi=n-1,ans=-1;
- while(lo<=hi)
- {
- int mid=(lo+hi+1)/2;
- if(arr[mid]==k){
- ans=mid;
- hi=mid-1;
- }
- else if(k<arr[mid])
- hi=mid-1;
- else
- lo=mid+1;
- }
- return ans;
- }
- int main()
- {
- int arr[7]={1,2,2,2,3,4,5};
- cout<<"lower bound of 2 is: ";
- cout<<l_bound(arr,7,2);
- }*/
- vector<int>vec;
- int lo_bound(int k)
- {
- int low_range=0,high_range=vec.size()-1,ans=-1;
- while(low_range<=high_range)
- {
- int mid_range=(low_range+high_range)/2;
- if(vec[mid_range]==k)
- {
- ans=mid_range;
- high_range=mid_range-1;
- }
- else if(k<vec[mid_range])
- {
- high_range=mid_range-1;
- }
- else
- low_range=mid_range+1;
- }
- return ans;
- }
- int main()
- {
- int n;
- cout<<"Enter how many element"<<endl;
- cin>>n;
- cout<<"Enter values are:"<<endl;
- while(n!=0)
- {
- int value;
- cin>>value;
- vec.push_back(value);
- n--;
- }
- cout<<"which value lower bound you want to find: ";
- int k;
- cin>>k;
- cout<<"Lower bound is: "<<lo_bound(k);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement