An element is a leader if it is strictly greater than every element that appears to its right. The rightmost element is always a leader (there is nothing to its right).
Count how many leaders the array has.
Line 1: an integer n.
Line 2: n space-separated integers (present whenever n >= 1).
One line: the number of leaders.
Example 1
Input
6 16 17 4 3 5 2
Expected
3
Explanation
Scanning from the right, 2, 5, and 17 each exceed everything after them; 17,5,2 are leaders -> 3 leaders.
Example 2
Input
4 1 2 3 4
Expected
1
Explanation
Only the last element 4 is greater than everything to its right, so there is 1 leader.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →