C Program
#include<stdio.h> int main() { int a[100], n, max=-1; scanf("%d", &n); for(int i=0; i<n; i++) scanf("%d", &a[i]); for(int i=n-1; i>=0; i--) if(a[i] > max) printf("%d ", a[i]), max = a[i]; }
C Output
Input: 6 16 17 4 3 5 2 Output: 2 5 17
C++ Program
#include<iostream> using namespace std; int main() { int a[100], n, max=-1; cin >> n; for(int i=0; i<n; i++) cin >> a[i]; for(int i=n-1; i>=0; i--) if(a[i] > max) cout << a[i] << " ", max = a[i]; }
C++ Output
Input: 7 5 3 20 15 8 3 2 Output: 2 3 8 20
JAVA Program
import java.util.*; class L { public static void main(String[] a) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), arr[] = new int[n], max = -1; for(int i=0; i<n; i++) arr[i] = sc.nextInt(); for(int i=n-1; i>=0; i--) if(arr[i] > max) { System.out.print(arr[i] + " "); max = arr[i]; } } }
JAVA Output
Input: 8 10 9 8 7 6 5 4 3 Output: 3 4 5 6 7 8 9 10
Python Program
a = list(map(int, input().split())) m = -1 for x in reversed(a): if x > m: print(x, end=" "); m = x
Python Output
Input: 7 10 4 10 6 5 2 Output: 2 5 6 10
In-Depth Learning – Whole Concept in Paragraphs
What Are Leaders in an Array?
In the context of arrays, a leader is a number which is strictly larger than all the numbers to its right. The very last number is always a leader since there are no numbers following it.
This is used to construct the logic of right-to-left traversal, a fundamental concept in most array-based problems. It's helpful in optimization problems, comparison, and filtering of data.
How the Code Works
We begin at the rightmost end of the array (last element). We initialize a variable, say max, with a small value like -1.
Then we read the array from right to left, and for each element:
If it's larger than max, it becomes a leader.
We replace max with this value for the next iteration.
Because we are scanning from the right, we compare every element once, and this is very efficient.
In Python, reversed(a) is used to iterate from the end to the start of a list in a neat manner.
Example
Input:
[16, 17, 4, 3, 5, 2]
Process (Right to Left):
2 → leader (no elements to the right)
5 > 2 → leader
3 < 5 → not a leader
4 < 5 → not a leader
17 > 5 → leader
16 < 17 → not a leader
Leaders:
2, 5, 17
(Printed in reverse because we scanned in reverse.)
Real-Life Analogy
You're in a row of people facing the sea. You can only look at people in front of you (to your right in a row). Anyone taller than all the people in front is visible — they are leaders.
This is analoguous to ranking, in which a person is a leader if no one performs better than him/her in the future.
Where and When Is It Used?
This phenomenon is applicable in:
Optimizing performance metrics
Stock analysis (detect peak or dominant prices)
Signal processing
Backward scanning problems
Leaderboard management
Interview rounds involving array logic
It's a popular question in coding rounds at Amazon, Infosys, TCS, Capgemini, Wipro, etc.
Time and Space Complexity
Operation\tComplexity
Time\tO(n) — single traversal
Space O(1) — in-place, no additional array used
This is the best solution. Some students incorrectly try nested loops (O(n²)) — this implementation is quite faster.
Python-Specific Tip
A neat Python solution with a list to reverse the leaders:
python
a = list(map(int, input().split()))
m, res = -1, []
for x in reversed(a):
if x > m: res.append(x); m = x
print(*reversed(res))
This prints leaders in original left-to-right order.
SEO-Optimized Natural Paragraph for Ranking
Want to get leaders of an array using C, C++, Java, or Python? This tutorial provides you with the most efficient and shortest possible code to get all the elements from an array that are larger than the elements on their right side. The leader problem is one of the most common interview problems, checking your skill in scanning arrays backwards, applying comparative logic, and minimizing time complexity. From ranking scales to financial summits, this idea has practical uses in stock market analysis, sorting data, and optimization. The ability to solve this problem reinforces your skills in array traversal, reverse thinking, and effective coding techniques.
Social Plugin