Data Structures & AlgorithmsVery hardGATE CS · competitive programming
One Lomuto partition pass with the last element as pivot runs on [3, 8, 2, 5, 1, 4, 7, 6]. What is the array after this pass, before any recursive call?
- A. [3, 2, 5, 1, 4, 6, 7, 8]
- B. [3, 2, 5, 1, 4, 8, 7, 6]
- C. [3, 2, 5, 1, 4, 6, 8, 7]
- D. [1, 2, 3, 4, 5, 6, 7, 8]
Correct answer: A. [3, 2, 5, 1, 4, 6, 7, 8]
Each element smaller than the pivot 6 is swapped to the front; the pivot is then swapped into index 5. Skipping that final swap leaves 8 at index 5, which is the trap.
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
- 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…
- 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…