Given an array of integers, print the leaders in the array.

Given an array of integers, Write a program to print all the LEADERS in the array. An element is leader if it is greater than all the elements to its right side. And the rightmost element is always a leader.

For example:
Consider the following Input Array: { 10, 34, 5, 15, 9, 2 }

Output: Leaders are- 34 15 9 2.

As 34, 15 , 9 are greater than all the elements to its right side and the rightmost element is always a leader so 2 is also a leader here. Check the following diagram to visualize the problem statement.

Algorithm to solve the problem :

• Start from the right keeping track of largest element (currentLeader). If a larger element is found, then print it and set currentLeader to the current element.
• We can consider the last element is a leader since there is no element to its right.
```class LeadersInArray
{
/* Java Function to print leaders in an array */
{
int max_from_right =  arr[size-1];

/* Rightmost element is always leader */
System.out.print(max_from_right + " ");

for (int i = size-2; i >= 0; i--)
{
if (max_from_right < arr[i])
{
max_from_right = arr[i];
System.out.print(max_from_right + " ");
}
}
}

/* Test above functions */
public static void main(String[] args)
{
int arr[] = new int[]{10,34,5,15,9,2};
int n = arr.length;
}
}```

Output & Time Complexity :

```Output : 34 15 9 2
Time Complexity: O(n)```

Arrays 12 Algorithm 12