Arrays January 13 ,2026

Leaders in an Array

Problem Statement

Given an array of integers, find all the leaders in the array.

An element is called a 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

arr = [16, 17, 4, 3, 5, 2]

Output

17 5 2

Explanation

  • 17 is greater than all elements to its right
  • 5 is greater than 2
  • 2 is the last element

Example 2

Input

arr = [7, 10, 4, 10, 6, 5, 2]

Output

10 10 6 5 2

Why This Problem Is Important

  • Common interview question
  • Tests:
    • Array traversal
    • Comparison logic
    • Right-to-left scanning
  • Forms the base for:
    • Suffix maximum problems
    • Stock buy-sell variants
    • Monotonic patterns

Approaches Overview

ApproachTime ComplexitySpace Complexity
Brute ForceO(n²)O(1)
Optimized (Right Scan)O(n)O(1)

Approach 1: Brute Force Method

Idea

For each element, check all elements to its right.
If no element on the right is greater, then the current element is a leader.

Algorithm

  1. Traverse array from index 0 to n-1
  2. Assume arr[i] is a leader
  3. Compare it with all elements to the right
  4. If any element is greater, it is not a leader
  5. Print all valid leaders

Time and Space Complexity

  • Time Complexity: O(n²)
  • Space Complexity: O(1)

C Implementation 

#include<stdio.h> 

int main() {
    int arr[] = {16, 17, 4, 3, 5, 2};
    int n = 6;

    for(int i = 0; i < n; i++) {
        int isLeader = 1;

        for(int j = i + 1; j < n; j++) {
            if(arr[j] > arr[i]) {
                isLeader = 0;
                break;
            }
        }

        if(isLeader)
            printf("%d ", arr[i]);
    }
    return 0;
}

Output

17 5 2

C++ Implementation 

#include<iostream> 
using namespace std;

int main() {
    int arr[] = {16, 17, 4, 3, 5, 2};
    int n = 6;

    for(int i = 0; i < n; i++) {
        bool isLeader = true;

        for(int j = i + 1; j < n; j++) {
            if(arr[j] > arr[i]) {
                isLeader = false;
                break;
            }
        }

        if(isLeader)
            cout << arr[i] << " ";
    }
    return 0;
}

Java Implementation 

public class LeadersBruteForce {
    public static void main(String[] args) {
        int[] arr = {16, 17, 4, 3, 5, 2};

        for(int i = 0; i < arr.length; i++) {
            boolean isLeader = true;

            for(int j = i + 1; j < arr.length; j++) {
                if(arr[j] > arr[i]) {
                    isLeader = false;
                    break;
                }
            }

            if(isLeader)
                System.out.print(arr[i] + " ");
        }
    }
}

Python Implementation 

arr = [16, 17, 4, 3, 5, 2]
n = len(arr)

for i in range(n):
    is_leader = True
    for j in range(i + 1, n):
        if arr[j] > arr[i]:
            is_leader = False
            break
    if is_leader:
        print(arr[i], end=" ")

JavaScript Implementation 

let arr = [16, 17, 4, 3, 5, 2];
let n = arr.length;

for (let i = 0; i < n; i++) {
    let isLeader = true;

    for (let j = i + 1; j < n; j++) {
        if (arr[j] > arr[i]) {
            isLeader = false;
            break;
        }
    }

    if (isLeader)
        process.stdout.write(arr[i] + " ");
}

.

Approach 2: Optimized Method (Right-to-Left Traversal)

Idea

  • Traverse the array from right to left
  • Keep track of the maximum element seen so far
  • If the current element is greater than or equal to this maximum, it is a leader

Algorithm

  1. Initialize maxRight = arr[n-1]
  2. Add maxRight to result
  3. Traverse from index n-2 to 0
  4. If arr[i] >= maxRight:
    • Update maxRight
    • Add element to result
  5. Reverse result for correct order

Time and Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1) (excluding output list)

C Implementation 

#include<stdio.h> 

int main() {
    int arr[] = {16, 17, 4, 3, 5, 2};
    int n = 6;

    int maxRight = arr[n - 1];
    int leaders[6], k = 0;

    leaders[k++] = maxRight;

    for(int i = n - 2; i >= 0; i--) {
        if(arr[i] >= maxRight) {
            maxRight = arr[i];
            leaders[k++] = arr[i];
        }
    }

    for(int i = k - 1; i >= 0; i--)
        printf("%d ", leaders[i]);

    return 0;
}

C++ Implementation 

#include<iostream> 
#include<vector> 
using namespace std;

int main() {
    int arr[] = {16, 17, 4, 3, 5, 2};
    int n = 6;

    vector leaders;
    int maxRight = arr[n - 1];
    leaders.push_back(maxRight);

    for(int i = n - 2; i >= 0; i--) {
        if(arr[i] >= maxRight) {
            maxRight = arr[i];
            leaders.push_back(arr[i]);
        }
    }

    for(int i = leaders.size() - 1; i >= 0; i--)
        cout << leaders[i] << " ";

    return 0;
}

Java Implementation 

import java.util.*;

public class LeadersOptimized {
    public static void main(String[] args) {
        int[] arr = {16, 17, 4, 3, 5, 2};

        List leaders = new ArrayList<>();
        int maxRight = arr[arr.length - 1];
        leaders.add(maxRight);

        for(int i = arr.length - 2; i >= 0; i--) {
            if(arr[i] >= maxRight) {
                maxRight = arr[i];
                leaders.add(arr[i]);
            }
        }

        Collections.reverse(leaders);
        for(int num : leaders)
            System.out.print(num + " ");
    }
}

Python Implementation 

arr = [16, 17, 4, 3, 5, 2]

leaders = []
max_right = arr[-1]
leaders.append(max_right)

for i in range(len(arr) - 2, -1, -1):
    if arr[i] >= max_right:
        max_right = arr[i]
        leaders.append(arr[i])

leaders.reverse()
print(*leaders)

JavaScript Implementation 

let arr = [16, 17, 4, 3, 5, 2];
let n = arr.length;

let leaders = [];
let maxRight = arr[n - 1];
leaders.push(maxRight);

for (let i = n - 2; i >= 0; i--) {
    if (arr[i] >= maxRight) {
        maxRight = arr[i];
        leaders.push(arr[i]);
    }
}

leaders.reverse();
console.log(leaders.join(" "));

Dry Run (Optimized Approach)

Array: [16, 17, 4, 3, 5, 2]

Start from right:
max = 2 → leader
5 > 2 → leader
17 > 5 → leader

Final leaders: 17 5 2

Summary

  • Leaders are elements greater than all elements to their right
  • Brute force is simple but inefficient
  • Optimized approach is preferred in interviews
  • Right-to-left traversal reduces time complexity to O(n)

Next Problems in the Series

Equilibrium Index of an Array

Sanjiv
0

You must logged in to post comments.

Related Blogs

Find the S...
Arrays February 02 ,2026

Find the Second Smal...

Find the S...
Arrays January 01 ,2026

Find the Sum of All...

Find the M...
Arrays January 01 ,2026

Find the Maximum Ele...

Find the M...
Arrays January 01 ,2026

Find the Minimum Ele...

Count Even...
Arrays January 01 ,2026

Count Even and Odd N...

Search an...
Arrays January 01 ,2026

Search an Element in...

Copy One A...
Arrays January 01 ,2026

Copy One Array into...

Reverse an...
Arrays January 01 ,2026

Reverse an Array

Print Alte...
Arrays January 01 ,2026

Print Alternate Elem...

Find the L...
Arrays January 01 ,2026

Find the Length of a...

Check if a...
Arrays January 01 ,2026

Check if an Array is...

Find the F...
Arrays January 01 ,2026

Find the First Eleme...

Find the L...
Arrays January 01 ,2026

Find the Last Elemen...

Count the...
Arrays January 01 ,2026

Count the Number of...

Replace Al...
Arrays January 01 ,2026

Replace All Elements...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at E...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at O...

Find the A...
Arrays January 01 ,2026

Find the Average of...

Count the...
Arrays January 01 ,2026

Count the Number of...

Remove Dup...
Arrays January 01 ,2026

Remove Duplicate Ele...

Move All Z...
Arrays January 01 ,2026

Move All Zeros to th...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by K...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by O...

Check if T...
Arrays January 01 ,2026

Check if Two Arrays...

Merge Two...
Arrays January 01 ,2026

Merge Two Sorted Arr...

Find Missi...
Arrays January 01 ,2026

Find Missing Number...

Find Dupli...
Arrays January 01 ,2026

Find Duplicate Eleme...

Count Freq...
Arrays January 01 ,2026

Count Frequency of E...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All U...
Arrays January 01 ,2026

Find All Unique Elem...

Insert an...
Arrays January 01 ,2026

Insert an Element at...

Delete an...
Arrays January 01 ,2026

Delete an Element fr...

Find the I...
Arrays January 01 ,2026

Find the Index of an...

Find Union...
Arrays January 01 ,2026

Find Union of Two Ar...

Find Inter...
Arrays January 01 ,2026

Find Intersection of...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s...

Find the L...
Arrays January 01 ,2026

Find the Largest Sum...

Kadane’s A...
Arrays January 01 ,2026

Kadane’s Algorithm (...

Two Sum Pr...
Arrays January 01 ,2026

Two Sum Problem

Subarray w...
Arrays January 01 ,2026

Subarray with Given...

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Rearrange...
Arrays January 01 ,2026

Rearrange Array Alte...

Equilibriu...
Arrays January 01 ,2026

Equilibrium Index of...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s,...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All P...
Arrays January 01 ,2026

Find All Pairs with...

Longest Co...
Arrays January 01 ,2026

Longest Consecutive...

Product of...
Arrays January 01 ,2026

Product of Array Exc...

Maximum Pr...
Arrays January 01 ,2026

Maximum Product Suba...

Find the F...
Arrays January 01 ,2026

Find the First Missi...

Count Inve...
Arrays January 01 ,2026

Count Inversions in...

Rearrange...
Arrays January 01 ,2026

Rearrange Array by S...

Check if A...
Arrays January 01 ,2026

Check if Array Can B...

Trapping R...
Arrays January 01 ,2026

Trapping Rain Water

Find Minim...
Arrays January 01 ,2026

Find Minimum in Rota...

Search in...
Arrays January 01 ,2026

Search in Rotated So...

Median of...
Arrays January 01 ,2026

Median of Two Sorted...

Merge Inte...
Arrays January 01 ,2026

Merge Intervals

Count Reve...
Arrays January 01 ,2026

Count Reverse Pairs

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Largest Re...
Arrays January 01 ,2026

Largest Rectangle in...

Maximum Su...
Arrays January 01 ,2026

Maximum Sum Rectangl...

Subarray S...
Arrays January 01 ,2026

Subarray Sum Equals...

Count Dist...
Arrays January 01 ,2026

Count Distinct Eleme...

Sliding Wi...
Arrays January 01 ,2026

Sliding Window Maxim...

Find K Max...
Arrays January 01 ,2026

Find K Maximum Eleme...

Minimum Nu...
Arrays January 01 ,2026

Minimum Number of Ju...

Chocolate...
Arrays January 01 ,2026

Chocolate Distributi...

Find All T...
Arrays January 01 ,2026

Find All Triplets Wi...

Kth Smalle...
Arrays January 01 ,2026

Kth Smallest Element...

Maximum Le...
Arrays January 01 ,2026

Maximum Length Biton...

Find the S...
Arrays February 02 ,2026

Find the Second Larg...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com