Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- /**int high_bound(int arr[],int n,int k)
- {
- int low=0,high=n-1,ans=-1;
- while(low<=high)
- {
- int middle=(low+high+1)/2;
- if(k==arr[middle])
- {
- ans=middle;
- low=middle+1;
- }
- else if(k>arr[middle])
- low=middle+1;
- else
- high=middle-1;
- }
- return ans;
- }
- int main()
- {
- int arr[7]={1,2,2,2,3,4,5};
- cout<<"Higher bound is: "<< high_bound(arr,7,2);
- }**/
- vector<int>vec;
- void uperBound(int k)
- {
- int low=0,high=vec.size()-1,ans=-1;
- while(low<=high)
- {
- int mid=(low+high+1)/2;
- if(k==vec[mid])
- {
- ans=mid;
- low=mid+1;
- }
- else if(k>vec[mid])
- low=mid+1;
- else
- high=mid-1;
- }
- cout<<ans;
- }
- int main()
- {
- int n,k;
- cout<<"Enter how many Element"<<endl;
- cin>>n;
- cout<<"Enter values"<<endl;
- while(n--)
- {
- int value;
- cin>>value;
- vec.push_back(value);
- }
- cout<<"which value you want to search";
- cin>>k;
- uperBound(k);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement