Smaller on Left | Practice | GeeksforGeeks

Given an array arr[ ] of N positive integers, the task is to find the greatest element on the left of every element in the array which is strictly smaller than itself, if this element does not exist for an index print "-1".
Example 1:
Input:
N = 5
arr[] = {2, 3, 4, 5, 1}
Output: 
-1 2 3 4 -1
Explanation:
Greatest element on the left of 3 smaller 
than itself is 2, for 4 it is 3 and for 5 
it is 1. Since 2 is the first element and 
no element on its left is present, so it's 
greatest smaller element will be -1 and for 
1 no element smaller than itself is present 
on its left, so it's greatest smaller element 
is -1.

Example 2:
Input:
N = 3
arr[] = {1, 2, 3} 
Output:
-1 1 2 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function Smallestonleft() that takes an array arr[ ] and sizeOfArray N, and return the required answer. The driver code takes care of the printing.
Expected Time Complexity: O(N*Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 106
1 ≤ arr[i] ≤ 108
0
sumitsinghrajput786gwalior2 weeks ago
public static int[] Smallestonleft (int arr[], int n) {       // Complete the function       int[] ans = new int[n];       TreeSet<Integer> ts = new TreeSet<>();       for(int i=0;i<n;i++){           Integer low = ts.lower(arr[i]);                      if(low==null){               ans[i]=-1;           }           else{               ans[i]=low;           }           ts.add(arr[i]);       }       return ans;   }
+1
shubhamrikhari86513 weeks ago
vector<int> Smallestonleft(int arr[], int n){   vector<int>ans;   set<int>s;   for(int i=0; i<n; i++) {       auto k = s.lower_bound(arr[i]);       if(k == s.begin()) {           ans.push_back(-1);       }       else {           k--;           ans.push_back(*k);       }       s.insert(arr[i]);   }   return ans;}
0
patildhiren443 weeks ago
JAVA - 
static int[] Smallestonleft (int arr[], int n) {
       TreeSet<Integer> ts = new TreeSet<>();
       
       int i=0;
       int[]res = new int[n];
       for(int ele : arr){
           Integer low = ts.lower(ele);
           
           if(low == null){
               res[i]=-1;
           }else{
               res[i]=low;
           }
           i++;
           
           ts.add(ele);
       }
       
       return res;
   }
+1
1ashishchauhan20021 month ago
vector<int> Smallestonleft(int arr[], int n){     vector<int>v(n,-1);   set<int>s;   for(int i=0 ;i<n; i++){       s.insert(arr[i]);       auto x = s.find(arr[i]);       if(x != s.begin()){           --x;           v[i] = *(x);       }   }   return v;      }
0
orton10421 month ago
#C++ Easy solution
vector<int> Smallestonleft(int arr[], int n){set<int> s;vector<int> v;for(int i=0;i<n;i++){   auto it=s.lower_bound(arr[i]);   if(it==s.begin())      v.push_back(-1);   else      {it--;      v.push_back(*it);}   s.insert(arr[i]);}return v;}
0
orton1042
This comment was deleted.
+1
abhishekjadhav91722 months ago
Can Anyone tell me why Stack is not working in this problem??
 
vector<int> Smallestonleft(int arr[], int n){     vector<int>brr(n,0);      stack<int> s;            for(int i = 0; i < n; i++)      {          if(s.empty())          {              brr[i] = -1;              s.push(arr[i]);          }          else          {              while(!s.empty())              {                  if(s.top() < arr[i])                  {                      brr[i] = s.top();                      s.push(arr[i]);                      break;                  }                  else                  {                      s.pop();                  }              }              if(s.empty())              {                  brr[i] = -1;                  s.push(arr[i]);              }          }                  return brr;  }
0
shubhankardey29sep2 months ago
//Using TreeSet, please upvote if u like the solution
class Complete{
    
   
    // Function for finding maximum and value pair
    public static int[] Smallestonleft (int arr[], int n) {
        // Complete the function
        TreeSet<Integer> set = new TreeSet<>();
        int []res = new int[n];
        int idx = 0;
        for(int i=0;i<n;i++)
        {
            if(set.size()==0 || arr[i]==1)
            res[idx++] = -1;
            
            else
            {
                int val = -1;
                int flag = 0;
                int t = arr[i]-1;
                if(set.contains(t))
                {
                    val = t;
                    flag = 1;
                }
                while(flag==0 && !set.contains(t) && t>0)
                {
                    if(set.contains(t-1))
                    {
                        val = t-1;
                        flag = 1;
                        break;
                    }
                    t--;
                }
                res[idx++] = val;
            }
            set.add(arr[i]);
        }
        return res;
    }
}
+4
starcode012 months ago
Can Anyone tell me why Stack is not working in this problem??
 
0
neerajkhatri1802 months ago
vector<int> Smallestonleft(int arr[], int n){   // Complete the function   vector<int> v;   stack<int> s;      for(int i=0;i<n;i++)   {       while(!s.empty() and arr[i]<=s.top())        s.pop();       if(s.empty())        v.push_back(-1);       else        v.push_back(s.top());       s.push(arr[i]);   }   return v;}
 
wrong answer reason
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.