- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find all leaders in an array in Java?
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.
As per the problem statement we have to find leaders in an array. An element is a leader if it is greater than all the elements to its right side.
Let’s start!
To Show You Some Instances
Instance-1
Suppose the original array is {16, 17, 4, 3, 11, 14, 23, 2, 6, 10}.
After finding leaders in an array the result will be − 23 10
Instance-2
Suppose the original array is {16, 17, 4, 3, 5, 6, 9, 1, 8, 2}.
After finding leaders in an array the result will be − 17 9 8 2
Instance-3
Suppose the original array is {1, 5, 7, 3, 2, 1}
After finding leaders in an array the result will be − 7 1
Algorithm-1: (Brute Force Approach)
Step 1 − Declare and initialize an integer array.
Step 2 − Initialize two for loop one inside another.
Step 3 − Take one element from the first element from the outer for loop.
Step 4 − Compare the element with its next elements by using an inner for loop.
Step 5 − If the outer for loop element remains greater till the last element of the array, then print that element.
Step 6 − Finally, you will get all the leaders printed in the console.
Algorithm-2: (Optimized Approach)
Step 1 − Declare and initialize an integer array.
Step 2 − Take for loop and start iterating the array from the rightmost element and track the max.
Step 3 − Anytime you get any new max element then the previous max element is a leader.
Step 4 − Finally, you will get all the leaders printed in the console.
Multiple Approaches
We have provided the solution in different approaches.
By Using Brute Force Approach (Left to Right Element Scan)
By Using Brute Force Approach with User Defined Method
By Using Optimized Approach (Right to Left Element Scan)
Let’s see the program along with its output one by one.
Approach-1: By Using Brute Force Approach
Example
We will solve this program by using Algorithm-1.
public class Main{ public static void main(String[] args){ //Declare and initialize the array elements int array[] = {16, 17, 4, 3, 5, 6, 9, 1, 8, 2}; int size = array.length; System.out.println("Leaders in the array are: "); //Logic Implementtaion for (int i = 0; i < size; i++){ int j; for (j = i + 1; j < size; j++){ if (array[i] <=array[j]) break; } if (j == size) // the loop didn't break{ //print the result System.out.print(array[i] + " "); } } }
Output
Leaders in the array are: 17 9 8 2
Approach-2: By Using Brute Force Approach with User Defined Method
Example
We will solve this program by using Algorithm-1 but here we will make use of a user defined method.
public class Main{ public static void main(String[] args){ //Declare and initialize the array elements int array[] = {16, 17, 4, 3, 5, 6, 9, 1, 8, 2}; int m = array.length; System.out.println("Leaders in the array are: "); //call a user defined method findLeaders(array, m); } //main method static void findLeaders(int array[], int size){ //Logic Implementtaion for (int i = 0; i < size; i++){ int j; for (j = i + 1; j < size; j++){ if (array[i] <=array[j]) break; } if (j == size) // the loop didn't break{ //print the result System.out.print(array[i] + " "); } } }
Output
Leaders in the array are: 17 9 8 2
Approach-3: By Using Optimized Approach
Example
We will solve this program by using Algorithm-2
public class Main{ public static void main(String[] args){ //Declare and initialize the array elements int array[] = {16, 17, 4, 3, 5, 6, 9, 1, 8, 2}; int m = array.length; System.out.println("Leaders in the array are: "); //call a user defined method findLeaders(array, m); } //main method static void findLeaders(int arr[], int size){ //Logic Implementtaion int rightMaximum=arr[arr.length-1]; //Here we have started loop from size-2 //as rightmost element is always a leader System.out.print(rightMaximum+" "); for (int i = size-2; i>=0; i--) { if(arr[i] > rightMaximum){ rightMaximum=arr[i]; System.out.print(rightMaximum+" "); } } } }
Output
Leaders in the array are: 2 8 9 17
In this article, we explored how to find leaders in an array by using Java.