# Leaders in an array in C++

The element in an array that is the greatest relative to all the items on its right side is known as the leader of the array.

According to this, the leader will always be the element on the right. The Leaders in array problem is essentially explained in this way.

The items in an array that are leaders in computer science are those that are greater than or equal to all of the elements to their right. To put it another way, a leader is an element in the array that has the highest value among all the elements to its right. Finding the leaders can be an effective operation because they frequently appear in different algorithmic challenges.

## What does a leader in an array represent?

A leader in an array is the element or elements that are largest relative to all other items in the array that are located on its right side. It implies that the leader will always be the element on the right. Therefore, an element is regarded as a leader if it is larger than every element to its right. It is attainable with the aid of loops. We can get the leader element by using two loops. The outermost loop selects each element one at a time from left to right while ranging from 0 to size-1. The inner loop compares the selected element to each element on its right side. If the chosen element is the largest of all the elements to its right, it is the leader.

Nested loops are used as the brute force solution to this leader in an array problem to check each element one by one. The inner loop assists in checking all the rightmost elements of that specific element while the outer loop aids in traversing from 0 to N - 1. The element will be printed if the prerequisite for being a leader is met.

### Algorithm

Step 1: Create the method "getResult", which will take two parameters: the input array and the size of the array, both in the range of "N".

Step 2: Use the "for" loop and the variable "i" to iterate from 0 to the array's size; the inner "for" loop will iterate from i + 1 to the array's size.

Step 3: End the loop if there is an element to the right of the element in the ith position of the array that is greater than the element in that position.

Step 4: The element in the ith position is the leader element, therefore we will print it if the value of "j" equals the array's size. Otherwise, we ignore the leader element and continue printing the rest of the elements.

### Program:

Let's take an example to demonstrate the leaders in an array in C++:

Output:

```Leaders in the array are: 2 5 17
```

### Explanation:

1. Taking an input vector of integers, and the findLeaders function finds the leaders.
2. It begins by presuming that the array's leader (leader = arr[n - 1]) is the member on the right of the array.
3. The next step is to loop through the array from right to left (from n-2 to 0), checking each element to see if it is greater than or equal to the leader.
4. It updates the leader and publishes it if the current element is greater than or equal to the leader.
5. The leaders are printed in reverse order because we begin with the rightmost element.
6. The findLeaders function is called in the main function along with the sample array 16, 17, 4, 3, 5, 2. What will result is: Leaders in the array are: 2 5 17
7. As a result of being greater than or equal to all entries in the array to their right, the leaders in this instance are 2, 5, and 17.

The above approach finds leaders in an array with an O(n) time complexity, where n is the array's element count. It is due to your single right-to-left traversal of the array.