81 81 votes An element in an array $X$ is called a leader if it is greater than all elements to the right of it in $X$. The best algorithm to find all leaders in an array solves it in linear time using a left to right pass of the array solves it in linear time using a right to left pass of the array solves it using divide and conquer in time $\Theta (n\log n)$ solves it in time $\Theta( n^2)$ Algorithms gatecse-2006 algorithms normal algorithm-design + – Rucha Shelke 30.7k views answer comment Share Follow Print See all 13 Comments 13 13 Comments reply Show 10 previous comments mo7ammedfarooq commented Nov 30, 2025 reply Follow flag Slight modification of Next greater element int curr_max = arr[n-1]; cout << arr[n-1] << " is a leader" << endl; // last element is always a leader for(int i = n - 2; i >= 0; i--){ if(arr[i] > curr_max){ cout << arr[i] << " is a leader" << endl; curr_max = arr[i]; } } 2 2 replyShare Ashutosh mandal commented Jan 8 reply Follow flag thank you very much 0 0 replyShare js__ commented Jan 28 reply Follow flag https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side 0 0 replyShare Please log in or register to add a comment.
Best answer 95 95 votes Option B. We can move from right to left, while keeping a note of the maximum element so far (let's call it current_max). Starting from the rightmost element, we initialize our current_max with it, since the rightmost element will always be a leader. Moving from right to left, if an element $x$ is greater than our current_max, then $x$ is also a leader. Add this element to list of leaders (or simply print it). Set current_max to $x$ and carry-on leftward. Time Complexity would be $\Theta(n)$. mdrwt answered Nov 21, 2014 • edited May 3, 2021 by soujanyareddy13 mdrwt comment Share Follow See all 22 Comments 22 22 Comments reply Show 19 previous comments pavansan commented Dec 31, 2024 reply Follow flag what will happen if we go from left to right can anybody pls explain? 0 0 replyShare Sarang_Gajare commented Aug 7, 2025 reply Follow flag The number of Comparision Will increase if we go from Left to Right, and it is asked "The Best Algo" 2 2 replyShare Ash24 commented Sep 4, 2025 reply Follow flag we need to ignore the ambiguity that first element will be leader or not i think.. ( though it does'nt affect the answer ) 0 0 replyShare Please log in or register to add a comment.
6 6 votes Ans: Option B. http://www.geeksforgeeks.org/leaders-in-an-array/ (Method 2). Prasanna answered Sep 4, 2015 • edited Oct 15, 2017 by kenzou Prasanna comment Share Follow 0 reply Please log in or register to add a comment.
5 5 votes Here is an example : Kshitij Sharma answered Dec 17, 2024 Kshitij Sharma comment Share Follow 0 reply Please log in or register to add a comment.
4 4 votes I think sorting the array using divide and conquer will be a better a idea so option c is correct. Bhagirathi answered Sep 19, 2014 • edited Oct 15, 2017 by kenzou 2 flags: ✌ Low quality (js__)✌ Low quality (Atharva_Shede) Bhagirathi comment Share Follow See all 4 Comments 4 4 Comments reply Arjun commented Oct 20, 2014 reply Follow flag No. Can be done in linear time. 2 2 replyShare Sandeep Suri commented Jan 11, 2017 reply Follow flag @Arjun Sir, Sir it can be done using counting sort also. 0 0 replyShare Abbas2131 commented Aug 20, 2017 reply Follow flag Sir, its asking to find ALL the leaders. Means for every element we have to ensure that all elements to the right are less than elemnt. In limear time we can only find a single leader. Wouldn't sorting be a better option? 0 0 replyShare Sandeep Suri commented Jan 9, 2018 reply Follow flag Yes it can work. And point to note here is leader is element which is greater than all element to it's right 0 0 replyShare Please log in or register to add a comment.
1 1 vote \\Ref: https://www.geeksforgeeks.org/leaders-in-an-array/ 1.#include <iostream> 2.using namespace std; 3.void printLeaders(int arr[], int size) 4.{ int max_from_right = arr[size-1]; 5. /* Rightmost element is always leader */ 6. cout << max_from_right << " "; 7. for (int i = size-2; i >= 0; i--) 8.{ if (max_from_right <= arr[i]) 9. { max_from_right = arr[i]; 10. cout << max_from_right << " "; }}} 11.int main() 12.{ int arr[] = {16, 17, 4, 3, 5, 2}; 13. int n = sizeof(arr)/sizeof(arr[0]); 14. printLeaders(arr, n); 15. return 0; } Till 5th line.6thstep o/p$=2$ for loop 1st time. o/p$=2\ 5$ for loop 2nd,3rd,4th time. o/p$=2\ 5\ 17$ for loop will execute 5th and 6th time also without making any changes and after that, the function will return. So $2,5,17$ are the leaders displayed in the o/p. Answer : B KUSHAGRA गुप्ता answered Sep 5, 2020 KUSHAGRA गुप्ता comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes I think, that this problem is only reverse sorting or putting elements in descending order, In order to do that start scanning from right. So. ANSWER is B. Jhaiyam answered Jul 5, 2020 Jhaiyam comment Share Follow 0 reply Please log in or register to add a comment.