Data Structures & AlgorithmsHard
Why does Floyd's method for finding a duplicate in an array of n+1 values in 1..n view the array as a linked structure?
- A. Each index points to the next index a[i], so a repeated value creates two paths into the same node, which forms a cycle whose entrance is the duplicate.
- B. The array is physically a linked list in memory.
- C. The values are sorted, so the structure is a tree.
- D. Each value points to its own index, so there is no cycle.
Correct answer: A. Each index points to the next index a[i], so a repeated value creates two paths into the same node, which forms a cycle whose entrance is the duplicate.
Following i → a[i] from the start must eventually enter a cycle because two indices share a target; the cycle's entry point is the duplicate value. Floyd's two pointers locate it in linear time with constant space.
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
- Stable sorting by two keys works by sorting twice. Which order of passes gives the lexicographic order with the first key as primary?
- Why does the median of two sorted arrays of sizes m and n not require merging both arrays fully?
- Which property makes the number of inversions of a rotated sorted array equal to a simple product?
- When merging two sorted lists with the standard two-pointer method, how many comparisons are made in the worst case?
- When computing the k-th smallest element of two sorted arrays, why can a binary search on the answer's position replace a full merge?
- Why do the positions of a stable partition by parity keep their relative order, and why is that important?