Leaders in an Array Problem Statement
You are given a sequence of numbers. Your task is to find all leaders in this sequence. A leader is defined as an element that is strictly greater than all the elements to its right in the sequence.
Input:
The first line of input contains an integer ‘T’, representing the number of test cases.
The first line of each test case contains an integer ‘N’, indicating the number of elements in the given sequence.
The second line of each test case contains ‘N’ space-separated integers, representing the elements in the sequence.
Output:
For each test case, print the leaders in the sequence, separated by spaces, on a new line.
Example:
Input:
T = 1
N = 5
Sequence = 13 14 3 8 2
Output:
14 8 2
Explanation:
In the given sequence, 14 is a leader because there are no greater elements on its right. Similarly, 8 and 2 are leaders. 13 and 3 are not leaders because they have greater elements on their right.
Constraints:
- 1 <= T <= 50
- 1 <= N <= 104
- -109 <= ELEMENTS[i] <= 109
Note:
The rightmost element is always a leader.
The order of elements in the output must be the same as the given sequence.
You don't need to print anything; just implement the function.
Be the first one to answer
Add answer anonymously...
Top Adobe Software Quality Engineer interview questions & answers
Popular interview questions of Software Quality Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app