Leaders in an array | Practice | GeeksforGeeks

Given an array A of positive integers. Your task is to find the leaders in the array. An element of array is leader if it is greater than or equal to all the elements to its right side. The rightmost element is always a leader. 
 
Example 1:
Input:
n = 6
A[] = {16,17,4,3,5,2}
Output: 17 5 2
Explanation: The first leader is 17 
as it is greater than all the elements
to its right.  Similarly, the next 
leader is 5. The right most element 
is always a leader so it is also 
included.

 
Example 2:
Input:
n = 5
A[] = {1,2,3,4,0}
Output: 4 0

 
Your Task:
You don't need to read input or print anything. The task is to complete the function leader() which takes array A and n as input parameters and returns an array of leaders in order of their appearance.
 
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)
 
Constraints:
1 <= n <= 107
0 <= Ai <= 107
0
navanee9981 day ago
//C# Solution
       public int[] leaders(int[] a, int n)       {           //Your code here           List<int> res = new List<int>();           int p = a[n-1];           res.Add(a[n-1]);                      for(int i = n-2; i>=0; i--)           {               if(a[i]>=p)               {                   res.Add(a[i]);                   p = a[i];               }           }                      res.Reverse();           return res.ToArray();       }
0
logesh7102 days ago
It will not accept (n*n) solution
 
 
 static ArrayList<Integer> leaders(int arr[], int n){
 	
 	    ArrayList<Integer> ans=new ArrayList<>();
        int leadersCount=1;
        int ind=n-2;
        int high=arr[n-1];
        
        //iterate array from backwards 
        // higher value put it in high pointer
        // decrement higher pointer
        
        for(int i=n-2;i>=0;i--){
            if(arr[i]>=high){
                high=arr[i];
                arr[ind--]=high;
                leadersCount++;
            }
        }
        /*
        [16, 17, 4, 3, 5, 2]  --  5 and 2 will putted in same place
        [16, 17, 4, 17, 5, 2] -- 17 is higher and put it higher place  
        [16, 17, 4, 17, 5, 2] -- decrement higher pointer
        */
        //For print counters
        for(int i=n-leadersCount;i<n;i++){
            ans.add(arr[i]);
        }
        return ans;
    }
 
 
+2
haarisjaved3 days ago
It's so annoying to see people spamming with their solution rather than giving a decent explanation ;))
0
amandeepnirania4 days ago
// accepted  vector<int> leaders(int a[], int n){       vector<int> v;       int test[n];       test[n-1] = a[n-1];       for(int i = n-2 ; i>=0 ;i--){           test[i]= max(a[i],test[i+1]);       }               for(int i =0 ;i<n; i++){            if(a[i]>=test[i])v.push_back(a[i]);        }       return v;       }
0
ahmedsamir99sh1005 days ago
//this solution accepted:
class Solution{   //Function to find the leaders in the array.   public:   vector<int> leaders(int a[], int n){       // Code here       int ans=0;       vector<int> v;       for(int i=n-1;i>=0;i--)       {           if(ans<=a[i])           {               ans=a[i];               v.push_back(ans);           }       }       reverse(v.begin(),v.end());       return v;   }};
////////////////////////////////////////////////////////////////////
//this solution is right but give you TLE:
class Solution{    //Function to find the leaders in the array.    public:    vector<int> leaders(int a[], int n){        // Code here        int sum;        int cnt;        int mx;        vector<int> v;        for(int i=0;i<n;i++)        {            sum=0;            cnt=0;            for(int j=i+1;j<n;j++)            {                mx=n-(i+1);                if(a[i]>=a[j])                {                    cnt++;                }            }            if(mx==cnt)            {                v.push_back(a[i]);            }        }        v.push_back(a[n-1]);        return v;    }};
0
mittaliraheja025 days ago
 vector<int>v;       int max_from_right=a[n-1];       v.push_back(a[n-1]);       for(int i=n-2;i>=0;i--){           if(a[i]>=max_from_right){               v.push_back(a[i]);               max_from_right=a[i];           }       }       //if(v.size()==0)return v;        reverse(v.begin(),v.end());        return v;   }
0
amitanand26 days ago
simple sol'n

class Solution{
    //Function to find the leaders in the array.
    public:
    vector<int> leaders(int a[], int n){
        // Code here
        vector<int>v;
        reverse(a , a+n);
        int c =0;
        v.push_back(a[0]);
        for(int i =1 ; i< n ; i++){
            // if(*max_element(v.begin() , v.end()) <= a[i]){
            //     v.push_back(a[i]);
            // }
            if(a[i] >= v[c]){
                v.push_back(a[i]);
                c++;
            }
        }
        reverse(v.begin() , v.end());
        return v;
        
    }
};
0
kuldeepahirwarmba6 days ago
it shows time limit exceed.
static ArrayList<Integer> leaders(int arr[], int n){       // Your code here       boolean isCheck = false;       ArrayList<Integer> result = new ArrayList<Integer>();       for(int i =0;i<n;i++)       {           int j = i;           while(j<n)           {               if (arr[i]>=arr[j])               {                   isCheck = true;                   j++;               }else{                   isCheck = false;                   break;               }           }           if(isCheck)           {               result.add(arr[i]);           }       }       return result;   }
+1
priyansh708901 week ago
    vector<int> leaders(int a[], int n){       // Code here       vector<int>ans;       int max = a[n-1];       ans.push_back(max);       for(int i = n-2;i>=0;i--)       {           if(max<=a[i])           {               max = a[i];               ans.push_back(max);           }       }       reverse(ans.begin(),ans.end());       return ans;   }
0
torq12841 week ago
C++ solution 
vector<int> leaders(int a[], int n){
        // Code here
        vector<int> v;
        int i,x=0;
        for(i=n-1;i>=0;i--)
        {
            if(x<=a[i])
            {
                x=a[i];
                v.push_back(x);
            }
        }
        reverse(v.begin(), v.end());
        return v;
        
        
    }
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.