Java Program to Print All the Leaders in an Array

This is the Java program to print all the leaders in an array.

Problem Description

Given an array of integers, find and print all the leaders of the array. A leader is defined as an element of the array which is greater than all the elements following it. The rightmost element is always a leader.
For example:
In the array {8, 7, 4, 3, 5, 2}, leaders are 8, 7, 5 and 2.
Explanation:

8 > 7, 8 > 4, 8 > 3, 8 > 5, 8 > 2.
7 > 4, 7 > 3, 7 > 5, 7 > 2.
5 > 2.
2 is the rightmost element.

Problem Solution

The idea is to traverse the array from right to left and keep track of the maximum element encountered. If the maximum element changes, then that element is a leader of the array.

Program/Source Code

Here is the source code of the Java Program to print all the leaders of an array. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. // Java program to print all the leaders in an array.
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class LeaderInAnArray {
  8.     //The main function to take input from the user
  9.     //and display the leaders in an array
  10.     public static void main(String[] args) {
  11.         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  12.         int size;
  13.         System.out.println("Enter the size of the array");
  14.         try{
  15.             size=Integer.parseInt(br.readLine());
  16.         }
  17.         catch(Exception e)
  18.         {
  19.             System.out.println("Invalid Input");
  20.             return;
  21.         }
  22.         int[] array=new int[size];
  23.         System.out.println("Enter array elements");
  24.         int i;
  25.         for(i=0;i<array.length;i++){
  26.             try{
  27.                 array[i]=Integer.parseInt(br.readLine());
  28.             }
  29.             catch(Exception e)
  30.             {
  31.                 System.out.println("An error occurred. Exiting");
  32.                 return;
  33.             }
  34.         }
  35.         System.out.println("The leaders of the array are");
  36.         \\ max variable is used to keep track of the current maximum element
  37.         int max=Integer.MIN_VALUE;
  38.         \\ loop to print all the leaders of the array
  39.         for(i=array.length-1;i>=0;i--) {
  40.             if (array[i] > max) {
  41.                 System.out.print(array[i] + "  ");
  42.                 max = array[i];
  43.             }
  44.         }
  45.     }
  46. }
Program Explanation

1. In function main(), firstly the user enters the array.
2. The variable max is initialized to Integer.MIN_VALUE.
3. The loop for(i=array.length-1;i>=0;i–) iterates through the array from right to left.
4. If any element, greater than max is found, then that element is displayed and the max variable is set to array[i].

advertisement
advertisement

Time Complexity: O(n) where n is the number of elements in the array.

Runtime Test Cases
 
 
Case 1 (Simple Test Case):
 
Enter the size of the array
6
Enter array elements
8
7
4
3
5
2
The leaders of the array are
2  5  7  8
 
Case 2 (Simple Test Case - reverse sorted array):
 
Enter the size of the array
10
Enter array elements
9
8
7
6
5
4
3
2
1
0
The leaders of the array are
0  1  2  3  4  5  6  7  8  9
 
Case 3 (Simple Test Case - sorted array):
 
Enter the size of the array
5
Enter array elements
1
2
3
4
5
The leaders of the array are
5

Sanfoundry Global Education & Learning Series – Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.