Java Program to Find all the Leaders in the Array

In the previous article, we have seen Java Program to Find the Second Smallest Number in an Array

In this article we are going to print all the leaders in an array in Java language.

Java Program to Find all the Leaders in the Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Note: Leaders are array elements which are greater than all the elements to its right.

Let’s see different ways to print all the leaders in an array.

Method-1: Java Program to Find all the Leaders in the Array By Static Initialization of Array Elements

Approach:

  • Create an array with elements.
  • Display the array to the user.
  • Use a for loop to traverse the array elements
  • Use another for loop to traverse the right side of the array
  • Break the loop if any next element is larger than the current element
  • Print the elements

Program:

import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        int arr[] = {12, 2, 34, 54, 20, 6};
        boolean flag;
        // Prints the array elements
        System.out.println("The array elements are"+Arrays.toString(arr));
        
        // Prints the leader elements
        System.out.print("The leaders in the array are: ");

        // Finds out the leader elements
        for(int i = 0; i < arr.length; i++)
        {
            flag = true;
            for(int next = i; next < arr.length; next++)
            {    if(arr[i]<arr[next])
                {   flag = false;
                    break;
                }
            }
            if(flag)
                System.out.print(arr[i]+" ");
        }
        
    }
}


Output:

The array elements are[12, 2, 34, 54, 20, 6]
The leaders in the array are: 54 20 6

Method-2: Java Program to Find all the Leaders in the Array By Dynamic Initialization of Array Elements

Approach:

  • Take input of array size.
  • Take input of array elements from the user.
  • Display the array to the user.
  • Use a for loop to traverse the array elements
  • Use another for loop to traverse the right side of the array
  • Break the loop if any next element is larger than the current element
  • Print the elements

Program:

import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        
        // Taking size as input from the user
        System.out.println("Enter the array size :");
        int size = scan.nextInt();
        
        // Creating the array
        int arr[] = new int[size];
        
        // Entering the array elements
        System.out.println("Enter array elements : ");
        for(int i=0;i<size;i++)
        {
            arr[i] = scan.nextInt();
        }
        
        // Prints the array elements
        System.out.println("The array elements are "+Arrays.toString(arr));

        boolean flag;
        // Prints the array elements
        System.out.println("The array elements are"+Arrays.toString(arr));
        
        // Prints the leader elements
        System.out.print("The leaders in the array are: ");

        // Finds out the leader elements
        for(int i = 0; i < arr.length; i++)
        {
            flag = true;
            for(int next = i; next < arr.length; next++)
            {    if(arr[i]<arr[next])
                {   flag = false;
                    break;
                }
            }
            if(flag)
                System.out.print(arr[i]+" ");
        }
        
    }
}


Output:

Enter the array size : 10
Enter array elements : 2 9 6 4 8 0 1 3 7 5
The array elements are [2, 9, 6, 4, 8, 0, 1, 3, 7, 5]
The array elements are[2, 9, 6, 4, 8, 0, 1, 3, 7, 5]
The leaders in the array are: 9 8 7 5

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: