Left most and right most index | Practice | GeeksforGeeks

Given a sorted array with possibly duplicate elements. The task is to find indexes of first and last occurrences of an element X in the given array.
Note: If the element is not present in the array return {-1,-1} as pair.
 
Example 1:
Input:
N = 9
v[] = {1, 3, 5, 5, 5, 5, 67, 123, 125}
X = 5
Output:
2 5
Explanation:
Index of first occurrence of 5 is 2
and index of last occurrence of 5 is 5.
Example 2:
Input:
N = 9
v[] = {1, 3, 5, 5, 5, 5, 7, 123, 125}
X = 7
Output:
6 6
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function indexes() which takes the array v[] and an integer X as inputs and returns  the first and last occurrence of the element X. If the element is not present in the array return {-1,-1} as pair.

Can you solve the problem in expected time complexity?

Expected Time Complexity: O(Log(N))
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 105
1 ≤ v[i], X ≤ 1018
0
vishutyagi73 days ago
c++ 0.23 sec sol
pair<long,long> p;       int n;       n=v.size();       int l=0,h=n-1,m,k=0,i,j;       while(l<=h)       {           m=(l+h)/2;           if(v[m]<x)           {               l=m+1;           }           else if(v[m]>x)           {               h=m-1;           }           else           {               k=1;               break;           }       }       if(k==1)       {           i=j=m;           while(v[i]==x&&i>=0)           {               i--;           }           i++;           while(v[j]==x&&j<n)           {               j++;           }           j--;           p=make_pair(i,j);           return p;       }       else       {           p=make_pair(-1,-1);           return p;       }
0
safe6 days ago
0(nlogn) cpp binary search solution
https://github.com/Pushpdeepsingh/Data-Structures/blob/main/Geeks%20For%20Geeks/Binary%20Search/Easy/Left%20most%20and%20right%20most%20index/code.cpp
0
shubhamkhavare2 weeks ago
      Simple java Soln: 
       long x1 = -1;       long x2 = -1;       for(int i = 0 ; i < v.length ; i++)       {           if(v[i] == x)           {               x1 = i;               break;           }       }       for(int i = 0 ; i < v.length ; i++)       {           if(v[i] == x)           {               x2 = i;           }       }       return new pair(x1, x2);
+1
harendraseervi1234567893 weeks ago
C++ solution
pair<long long ,long long> indexes(vector<long long> v, long long x)
   {
       pair<long long ,long long>ans(-1,-1);
       long long l=0,r=v.size()-1;
       while(l<=r){
           long long mid=(l+r)/2;
           if(v[mid]<x){
               l=mid+1;
           }
           else if(v[mid]>x){
               r=mid-1;
           }
           else{
               l=mid;
               r=mid;
               while(l>=0 && v[l]==x){
                   l--;
               }
               while(r<v.size() && v[r]==x){
                   r++;
               }
               break;
           }
       }
       if(l>r){
           return ans;
       }
       ans.first=l+1;
       ans.second=r-1;
       return ans;
   }
0
saurabh kundu4 weeks ago
Java solution:
public pair indexes(long v[], long x)   {       Map<Long, List<Integer>> map = new HashMap<>();       for(int i =0; i<v.length; i++) {           if(map.containsKey(v[i])) {               map.get(v[i]).add(i);           }else {               List<Integer> list = new ArrayList<>();               list.add(i);               map.put(v[i], list);           }       }       int start = -1;       int end = -1;       if(null != map.get(x)) {           List<Integer> list = map.get(x);           start = list.get(0);           end = list.get(list.size()-1);       }       return new pair(start, end);   }
0
swapniltayal4221 month ago
    pair<long ,long> indexes(vector<long long> v, long long x)   {       // code here       int f = -1;       int l = -1;       pair<long, long>v1;       for (int i=0; i<v.size() ;i++){           if (v[i] == x && f == -1){               f = i;           }if(v[i] == x){               l = i;           }       }              v1.first = f;       v1.second = l;       return v1;   }
+1
himanshukug19cs1 month ago
java solution
 
    int low=0;       int high=v.length-1;   pair ans= new pair(-1,-1);   if(x<v[0]||x>v[v.length-1])       return ans;   int index=-1;   while(low<=high){       int mid=(low+high)/2;       if(v[mid]==x){           index=mid;           break;       }       if(v[mid]>x)           high=mid-1;       if(v[mid]<x)           low=mid+1;                 }   if(index!=-1){       int temp=index;       while(temp>=0&&v[temp]==x){           temp--;       }       ans.first=Long.valueOf(temp+1);       temp=index;       while(temp<v.length&&v[temp]==x){           temp++;       }       ans.second=Long.valueOf(temp-1);   }   return ans;
+1
anandgeeky1 month ago
JAVA CODE:
 
 int left = 0, right = 0, p = 1;              for(int i=0;i<v.length;i++){           if(v[i] == x){               left = i;               p = p*2;               break;           }       }       for(int i=v.length-1; i>=0;i--){           if(v[i] == x){               right = i;               p = p*2;               break;           }       }              if(p != 4)           return new pair(-1,-1);      return new pair(left,right);
0
fromtheast11 month ago
C++, 0.2s, time O(log(n)), space O(1)
 
    pair<long,long> indexes(vector<long long> v, long long x)
    {
        int mid = (v.size() - 1) / 2;
        int l = 0;
        int r = v.size() - 1;
        pair<long, long> res(-1, -1);
        
        // Find the 1st x in the list
        while (l <= r) {
            if (v[mid] == x) {
                res.first = mid;
                res.second = mid;
                break;
            }
                
            if (v[mid] > x) {
                r = mid - 1;
            } else if (v[mid] < x) {
                l = mid + 1;
            }
            
            mid = l + (r-l)/2;
        }
        
        int r1, l1;
        
        // Find the most right x
        if ( (res.second < (v.size() - 1)) && v[res.second + 1] == x) {
            r1 = r;
            l1 = res.second + 1;
            mid = l1 + (r1 - l1)/2;
            while (l1 <= r1) {
                if (v[mid] > x) {
                    r1 = mid - 1;
                } else {
                    l1 = mid + 1;
                }
                
                mid = l1 + (r1-l1)/2;
            }
            res.second = l1 -1;
        }
        
        // Find the most left x
        if ( (res.first > 0) && v[res.first - 1] == x) {
            r1 = res.first - 1;
            l1 = l;
            mid = l1 + (r1 - l1)/2;
            
            while (l1 <= r1) {
                if (v[mid] < x) {
                    l1 = mid + 1;
                } else {
                    r1 = mid - 1;
                }
                
                mid = l1 + (r1 - l1)/2;
            }
            res.first = r1 + 1;
        }
        
        return res;
        
    }
0
dhanshreekadam002 months ago
pair<long,long> indexes(vector<long long> v, long long x)   {       if(binary_search(v.begin(),v.end(),x))             return{lower_bound(v.begin(),v.end(),x)-v.begin(),upper_bound(v.begin(),v.end(),x)-v.begin()-1};       else           return {-1,-1};   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.