Problem Link - Leader of an Array in Arrays
Problem Statement:
Given an array A of length N. We have to print all the leaders in the array. An element is a leader if it is strictly greater than all the elements to its right side. And the rightmost element is always a leader.
Approach:
- For each element
A[i]
in the array:- The inner loop checks if there is any element to the right of
A[i]
that is greater than or equal toA[i]
. If such an element exists, the loop breaks. - If no element to the right is greater than or equal to
A[i]
, it meansA[i]
is a leader.
- The inner loop checks if there is any element to the right of
Complexity:
- Time Complexity:
O(N^2)
using two nested loops - Space Complexity:
O(1)
No extra space is used to store the result.