Data Structures & AlgorithmsModerateGATE CS · DSA
Find the leaders (elements strictly greater than everything to their right) by scanning from the right. What is printed?
Find the leaders (elements strictly greater than everything to their right) by scanning from the right. What is printed?
a = [17, 8, 6, 15, 10, 12, 6, 12, 3]
best, out = float("-inf"), []
for x in reversed(a):
if x > best: out.append(x); best = x
print(out[::-1])- A. [17, 15, 12, 12, 3]
- B. [17, 15, 3, 12]
- C. [17, 12, 15, 3]
- D. [17, 15, 12, 3]
Correct answer: D. [17, 15, 12, 3]
Scanning right to left, an element is a leader when it beats the running maximum; ties are not leaders under the strict rule. Output: [17, 15, 12, 3].
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Count distinct values in every window of size k with a frequency map. What is printed? python from collections import Counter a, k = [2…
- Quickselect returns the k-th smallest element of an unsorted array in expected O(n) time. For [14, 4, 8, 46, 16, 34, 31, 40] and k = 3…
- How many contiguous subarrays of [6, 4, 2, 5, 6, 2, 3, 3] have a product strictly less than 30? (Positive values: a two-pointer window…
- Rotate a linked list to the RIGHT by k places (k may exceed the length). What is printed? python def rotate(h, k): n, t = 1, h while t.nx…
- Remove the n-th node from the END of a linked list in one pass, using a dummy node and a gap of n between two pointers. What is printed?…
- Delete duplicates from a SORTED linked list. Version A keeps one copy of each value; version B removes every value that appears more than…