GATE / CS ARCHIVE

GATE CSE 2006 / QUESTION 17

Array leaders and traversal direction

Algorithms · Correctness, Invariants & Design · Full GATE CSE 2006 paper

GATE 2006, Q17 1 mark · MCQ
Report issue

Correctness, Invariants & Design

Question 17, GATE 2006: An array element is a leader if it exceeds every element to its right. Which description gives the best algorithm for finding all leaders? No auxiliary-space restriction is stated, which affects whether the options are exclusive.
Options:
(A) Linear time with a left-to-right pass
(B) Linear time with a right-to-left pass
(C) Divide and conquer in Θ(n log n)
(D) Θ(n²) time
Tap to enlarge
Your answer
Answer & solution

Answer: B with constant extra space; A also possible with a stack (no official key was published)

1. Scanning right to left while retaining the largest value seen reports exactly the elements strictly larger than every element to their right. This is Θ(n) time and O(1) extra working space, yielding the usual B.

2. However A is also achievable in linear time if extra storage is allowed: scan left to right, keep candidate leaders in a decreasing stack, and pop every prior candidate≤the new value before pushing it. Each entry is pushed/popped at most once; the final stack contains the leaders.

3. Since the prompt does not require constant space, traversal direction alone does not make B uniquely correct.

4. Teaching check: Right-to-left scanning initializes the current maximum with the rightmost element and reports a new element only when it is strictly greater. Equal values do not become leaders if an equal value occurs later. A left-to-right monotone stack is a separate linear-time possibility, explaining the nonunique direction-only options.

Original paper, page 2