Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

LEADERS IN ARRAY

INTRODUCTION
• A leader in an array is the element of the array that is greatest
compared to all the elements residing on the right side of that
element.
• According to this, the rightmost element will always be the
leader. This is the basics of the Leaders in array problem.

5 6 7 0 1 3 2

LEADER LEADER LEADER


What is a leader in an array?

▪ A leader in an array is the elements of the array that is greatest compared to all
the elements residing on the right side of that element.
▪ According to this, the rightmost element will always be the leader. So if an
element is bigger than every other element to its right, it is considered a leader.
▪ It can be obtained with the help of loops. Using two loops, we can get the leader
element.
▪ The outermost loop goes from 0 to size–1 and selects each element one at a
time from left to right.
▪ The selected element is compared to every element on its right side by the inner
loop.
▪ The chosen element is the leader if it is larger than all of the elements to its
right.
PROBLEM STATEMENT
Given an array arr of size n with all unique elements. Print all Leaders in
the array in any order.
Example:
Input: [2,4,6,3,1,2] Output: 6,3,2
Explanation:
In the first example, where Input: [2,4,6,3,1,2], Output: 6,3,2, we can
see that the leaders are 6, 3, and 2 because
• 2 is the rightmost element, so it is a leader.
• 3 is greater than all the elements on its right side i.e. 3 is greater than 1 and 2,
so it is a leader.
• 6 is greater than all the elements on its right side i.e. 6 is greater than 3, 1, and
2, so it is a leader.
METHOD TO FIND THE LEADER
▪ This is the Brute-Force method to find leaders in an array.
▪ In this approach, we will compare every element of the array to
all the elements which are on its right side, if this element is
greater than all the elements which are on its right side, then
this element is one of the leaders in the array.
▪ We will use two nested loops, the outer loop will run n times
and each time we will pick an element and see all the elements
on its right side.
▪ If this element is greater than all the elements to its right side,
then it is a leader and we will print it.
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.
CODING
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 Implementaion
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
THANK YOU

You might also like