Find leaders in an array



We need to print all the leaders present in the array. Element is the leader if it is greater than right side of elements.
For example:


Solution 1:

Use two loops. Outer loop to iterate over array elements and inner loop to check for right elements of the array. If current element is greater than right elements than it is a leader.
Java code:

Time complexity : o(N^2)

Solution 2:

Lets find more optimized solution
We will use property that rightmost element is always a leader.

  • We will start from rightmost element and track max.
  • Whenever we get new max, that element is a leader.

Java code:

Time complexity : o(N)

Java Program to find leaders in the array:

When you run above program, you will get below output:


Add Comment