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
madanlalsalvi20012 hours ago
#Simple C++ solution 
 
    vector<int> leaders(int a[], int n){       // Code here       vector<int> v;       int max=a[n-1];       for(int i=n-1;i>=0;i--){           if(a[i]>=max)           {               max=a[i];               v.push_back(max);           }                  }         reverse(v.begin(), v.end());         return v;   }
0
vishaltuqz18 hours ago
Simple Easy to understand Java code
class Solution{   //Function to find the leaders in the array.   static ArrayList<Integer> leaders(int arr[], int n){             ArrayList<Integer> res = new ArrayList<>();              int max = -1;              for ( int i = n-1; i >= 0; i--){                      if (arr[i] >= max){               max = arr[i];               res.add(max);           }                 }       Collections.reverse(res);       return res;          }}
+1
surendramkishor2 days ago
#Very Easy Python Solution for understand

    def leaders(self, A, N):
        #Code here
        Max = 0
        leaders = []
        for item in reversed(A):
            if item >= Max:
                leaders.append(item)
                Max = item
        return reversed(leaders)
0
aditya1h2o2 days ago
C++ solution
class Solution{   //Function to find the leaders in the array.   public:   vector<int> leaders(int a[], int n){       // Code here       int max=a[n-1];       vector<int>A;       A.push_back(a[n-1]);       for(int i=n-2;i>=0;i--){           if(a[i]>=max){               A.push_back(a[i]);               max=a[i];           }       }       reverse(A.begin(),A.end());       return A;   }}; 
0
rushabhkalmate2 days ago
class Solution{   //Function to find the leaders in the array.   public:   vector<int> leaders(int a[], int n){       int maxi=INT_MIN;       vector<int>ans;       for(int i=n-1;i>=0;i--){           if(a[i]>=maxi){               ans.push_back(a[i]);               maxi=max(a[i],maxi);           }       }       reverse(ans.begin(), ans.end());          return ans;   }};
0
mokalevaibhav3 days ago
MOST EFFICIENT C++ SOLUTION : 
vector<int> leaders(int a[], int n)
{
        int max=0;
        vector<int> v;
        for(int i=n-1;i>=0;i--)
        {
            
            if(max<=a[i])
            {
                max=a[i];
                v.push_back(max);
            }
        }
        int s=v.size();
        for(int i=0;i<s/2;i++)
        {
            v[i]^=v[s-i-1]^=v[i]^=v[s-1-i];
        }
        //reverse(v.begin(),v.end());
        return v;
    }
0
shivamdubeyagra84 days ago
//            Time  =  1.19/2.51                 (Java Solution)
 
class Solution{
   static ArrayList<Integer> leaders(int arr[], int n){
       ArrayList<Integer> ans = new ArrayList<>();       ans.add(arr[n-1]);              int leader = arr[n-1];              for(int i=n-2; i>=0; i--)       {           if(arr[i] >= leader)           {               ans.add(arr[i]);               leader = arr[i];           }       }             Collections.reverse(ans);       return ans;   }}
0
cu15bcs15875 days ago
static ArrayList<Integer> leaders(int arr[], int n){
        // Your code here
        ArrayList<Integer> res = new ArrayList<>();
        int t1=0,t2=arr[n-1];
        res.add(arr[n-1]);
        for(int i = n-2; i >= 0; i--) {
            t1 = arr[i];
            t2 = Math.max(t1,t2);
            
            if(t1 == t2) res.add(t1);
        }
        Collections.reverse(res);
        return res;
    }
0
swatidubey9145 days ago
JAVA easy solution , Upvote if helpfull.
 
class Solution{   //Function to find the leaders in the array.   static ArrayList<Integer> leaders(int arr[], int n){       ArrayList<Integer> al = new ArrayList<>();       Stack<Integer> st = new Stack<>();       st.push(arr[n-1]);       int max = arr[n-1];              for(int i = n-2 ; i >= 0 ; i--){           if(arr[i] >= max){               st.push(arr[i]);               max = arr[i];           }       }       while(st.size() > 0){           al.add(st.pop());       }       return al;   }} 
0
swaptheshinigami5 days ago
class Solution{   //Function to find the leaders in the array.   static ArrayList<Integer> leaders(int arr[], int n){       ArrayList<Integer> leaders =new ArrayList<Integer>();       leaders.add(arr[n-1]);       int leader=arr[n-1];       for(int i=n-2;i>=0;i--){           if(arr[i]>=leader){           leaders.add(arr[i]);           leader=arr[i];           }       }       Collections.reverse(leaders);        return leaders;   }}
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.