Javatpoint Logo
Javatpoint Logo

Find Leader in the Given Array

In this tutorial, we will write the Python code to find the leader elements in the given array. A leader refers to an element in an array that is greater than or equal to all the elements to its right side. In other words, an element is considered a leader if it is larger than or equal to every element that comes after it in the array.

Let's understand the following example -

Example -

Explanation -

17 is greater than all elements to its right (4, 3, 5, 2).

5 is greater than all elements to its right (2).

2 is the rightmost element, so it is always considered a leader.

So, the leaders in this array are [17, 5, 2]

Example -

Explanation -

4 and 0, as they are greater than or equal to all the elements to their right side.

Let's understand the solution of the above problem.

Solution

We will solve this problem using the various approaches.

Naïve Approach

  1. Initialize an empty list to store the leaders.
  2. Start iterating through the array from the leftmost element to the second-to-last element (excluding the last element, as it is always a leader).
  3. For each element A[i] at index i:
    1. Initialize a flag variable is_leader to True, assuming A[i] is initially considered a leader.
    2. Start iterating through the elements to the right of A[i], from A[i+1] to the last element.
    3. For each element A[j] to the right of A[i]:
      If A[j] is greater than or equal to A[i], continue checking the next element.
      If A[j] is less than A[i], set is_leader to False and break out of the loop because A[i] cannot be a leader.
    4. After checking all the elements to the right of A[i], if is_leader is still True, it means that A[i] is a leader. Append A[i] to the list of leaders.
  4. After the loop finishes, the list of leaders will contain all the leaders found in the array.

Let's understand the following example -

Example -

Output:

Leaders in the array: [4, 0]

Explanation -

We define a function find_leaders() that takes an array arr as input.

We initialize empty list leaders to store the leaders found in the array.

We traverse the array from left to right using two nested loops. The outer loop iterates through elements from the first to the second-to-last element, and the inner loop compares each element with elements to its right.

We use a Boolean variable is_leader to assume that the current element is a leader initially.

Inside the inner loop, if we find any element greater than the current element, we set is_leader to False and break out of the loop because the current element cannot be a leader.

If is_leader is still true after checking all elements to the right, it means the current element is a leader, so we append it to the leaders list.

Finally, we append the rightmost element (last element) to the leaders list because it is always a leader.

The time complexity of the naive approach to find leaders in an array is O(n^2), and the space complexity is O(1).

Approach - 2: Find Leader by finding suffix maximum

Finding the suffix maximum in an array means identifying the maximum value among the elements to the right of a specific element in the array, including the element itself.

Below is the approach of suffix maximum.

Start by initializing a variable to keep track of the maximum value encountered while traversing the array from right to left.

Begin iterating through the array from right to left.

At each step, compare the current element with the maximum value encountered so far (initialized in step 1).

If the current element is greater than the maximum value, update the maximum value to be the current element because it represents the maximum value to the right of the current element.

Continue this process for all elements in the array, effectively finding the maximum value in the suffix (elements to the right) for each element.

As you traverse the array, record or print the maximum value encountered for each element. These recorded values represent the suffix maximums for the corresponding elements in the original array.

Let's understand the following example -

Example -

Output:

Leaders in the array: [17, 5, 2]

Explanation -

We define a function find_leaders that takes an array arr as input.

We find the length of the array n and initialize empty list leaders to store the leader elements.

We start by initializing the max_right variable with the rightmost element of the array, which is arr[n - 1]. The rightmost element is always a leader, so we add it to the leaders list.

We then iterate through the array from the second-to-last element (index n - 2) to the first element (index 0) in reverse order (from right to left).

Inside the loop, we compare each element with the current max_right. If the current element is greater than or equal to max_right, it becomes the new leader, and we update max_right accordingly.

We add each leader element to the leaders list as we encounter them.

After the loop, we reverse the leaders list to obtain the elements in their original order.

Finally, we return the leaders list containing all the leader elements in the array.

In the example usage, we call the find_leaders function with an example array and print the leaders found in the array.

Approach - 3: Stack-Based Approach

The previous approach provided a linear time complexity, but it didn't maintain the order of elements as they appear in the input array. To preserve the original order of elements while finding leaders, we can employ a stack data structure.

Here are the revised steps for identifying leaders in an array using a stack-based approach:

  1. Begin the process from the last index of the array. The rightmost element is always a leader since there are no elements to its right.
  2. Proceed to iterate through the array in reverse order, moving towards index position 0.
  3. Maintain a record of the maximum value encountered as you traverse the array.
  4. Whenever you encounter an element greater than the previously recorded maximum value, consider it a leader and push it onto a stack.
  5. After completing the iteration, go through the stack and print its contents, as these elements represent the leaders in the original array.

Let's understand the following example.

Example -

Output:

4 0

Explanation -

We define a function find_leaders that takes an array arr as input.

We calculate the length of the input array n and initialize an empty stack stack to store the leaders.

We initialize max_element as the last element of the input array because the rightmost element is always a leader. We push this maximum element onto the stack to start.

We then iterate through the array from the second-to-last element (index n-2) to the first element (index 0) in reverse order using a for loop.

Inside the loop, we compare the current element arr[i] with the max_element. If the current element is greater than or equal to the max_element, it is considered a leader, so we push it onto the stack. We also update max_element to the current element if it's greater.

After iterating through the array, we have identified and stored the leaders in the stack.

Finally, we print the leaders in the same order as they appear in the input array by popping elements from the stack and printing them.

Conclusion

In this tutorial, we learned how to find leader elements in an array, which are elements greater than or equal to all elements to their right. We explored three different approaches such as naïve approach which involved iterating through the array and comparing each element with those to its right. It has a time complexity of O(n2). Finding leaders by finding suffix maximum where we optimized the process by finding the maximum element to the right of each element. It achieved a linear time complexity of O(n) and preserved the order of elements. And Stack-Based Approach which is used to to maintain the original order of elements, we used a stack. Starting from the rightmost element, we iterated through the array in reverse, pushing leaders onto the stack. This method also had a time complexity of O(n).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news