Data Structures & AlgorithmsHard
Values in 4 3 2 7 8 2 3 1 lie in the range 1 to 8 (length 8). Using cyclic sort (place each value v at index v−1 by swaps), which values of 1..8 are MISSING from the array? Give them in increasing order.
- A. 5 6 0
- B. 1 2 3 4 7 8
- C. 5 6
- D. 6 5
Correct answer: C. 5 6
After cyclic placement each position k holds value k+1 when it is present; the positions still wrong identify the missing values. Taking the complement of the array as a set gives the same answer, so an incorrect comparison (like sorting the input) is what causes mistakes here.
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
- Find the duplicate in 2 5 9 6 9 3 8 9 7 1 (values 1 to 9, one repeated) with Floyd's method: slow = a[slow] and fast = a[a[fast]], both…
- What is the largest difference between two CONSECUTIVE values after sorting 3 6 9 1 2 15 in ascending order?
- Selection sort (ascending) is run on 7 2 9 4 1 6, swapping the minimum of the unsorted part into place in each pass, and skipping the swap…
- Sort 7 8 3 12 5 6 15 4 by the NUMBER OF 1-BITS in binary, ascending; values with the same bit count are ordered by value ascending. Give…
- A queue holds 1 2 3 4 5 6 7 8 from front to back. Interleave it: take the front element of the first half, then the front of the second…
- Starting from an empty stack, perform in order: push 4, push 9, pop, push 2, push 7, pop, pop, push 5. Reading from the bottom to the top…