#include<bits/stdc++.h>
#define ll long long int
using namespace std;

ll lowerindex(ll a[],ll n,ll key){
    ll low=0;
    ll high=n-1;
    ll res=-1;
    while(low<=high){
        ll mid=(low+high)/2;
        if(a[mid]==key){
            res=mid;
            high=mid-1;
        }
        else if(a[mid]>key){
            high=mid-1;
        }
        else{
            low=mid+1;
        }
    }
    return res;
}

ll upperindex(ll a[],ll n,ll key){
    ll low=0;
    ll high=n-1;
    ll res=-1;
    while(low<=high){
        ll mid=(low+high)/2;
        if(a[mid]==key){
            res=mid;
            low=mid+1;
        }
        else if(a[mid]>key){
            high=mid-1;
        }
        else{
            low=mid+1;
        }
    }
    return res;
}

int main(){
    ll n;
    cin>>n;
    ll a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    ll key;
    cin>>key;
    ll lower = lowerindex(a,n,key);
    ll upper = upperindex(a,n,key);
    if(lower == -1 && upper == -1){
        cout<<"-1";
    }
    else{
        cout<<lower<<" "<<upper;
    }
    cout<<"\n";
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: