Data Structures & AlgorithmsVery hardGATE CS · competitive programming
A three-way partition (Dutch national flag) with a low pointer, a mid pointer and a high pointer runs on [2, 0, 2, 1, 1, 0]. What is the final array?
- A. [0, 0, 2, 2, 1, 1]
- B. [0, 0, 1, 1, 2, 2]
- C. [2, 2, 1, 1, 0, 0]
- D. [0, 1, 1, 0, 2, 2]
Correct answer: B. [0, 0, 1, 1, 2, 2]
After a 2 is swapped to the high end, mid must not advance, because the value swapped in is still unexamined. The result is the sorted order 0s, 1s, 2s.
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
- What is the smallest positive integer missing from the array [3, 4, −1, 1]?
- What is the worst-case time complexity of finding the minimum in a rotated sorted array that may contain duplicates, using a binary search…
- For binary search over 1000 distinct sorted values with mid = (lo + hi) / 2 and the loop running while lo ≤ hi, what is the largest number…
- In a Fenwick tree with n = 16, an update at index 5 moves i ← i + (i & −i) while i ≤ n. How many tree nodes does the update touch?
- Brian Kernighan's loop (x &= x − 1 until x is 0) counts the set bits of x. What is the total number of loop iterations when it is run once…
- How many integers x in [0, 1023] have no two adjacent 1 bits in their 10-bit binary form?