Practice
n containing numbers from 1 to n with exactly one number duplicated and one number missing. Which algorithmic approach guarantees finding both the duplicate and missing numbers in O(n) time and O(1) extra space?Solution
Step 1: Understand problem constraints
The array has numbers from 1 to n with one duplicate and one missing number, so placing each number at its correct index reveals the corrupt pair.Step 2: Identify the best approach
Cyclic sort places each number at index = number - 1 in O(n) time and O(1) space, enabling detection of the mismatch directly.Final Answer:
Option A -> Option AQuick Check:
Cyclic sort is optimal for this problem pattern [OK]
- Assuming sorting is always optimal
- Confusing DP with counting
- Using greedy without index placement
findDuplicate([2, 1, 4, 3, 2])?Solution
Step 1: Trace initial array and i=0
nums = [2,1,4,3,2], i=0, correct_index=1. nums[0]=2 != nums[1]=1, swap -> nums=[1,2,4,3,2]Step 2: i=0 again, correct_index=0, nums[0]=1 == nums[0]=1, i=1
i=1, correct_index=1, nums[1]=2 == nums[1]=2, i=2Step 3: i=2, correct_index=3, nums[2]=4 != nums[3]=3, swap -> nums=[1,2,3,4,2]
i=2 againStep 4: i=2, correct_index=2, nums[2]=3 == nums[2]=3, i=3
i=3, correct_index=3, nums[3]=4 == nums[3]=4, i=4Step 5: i=4, correct_index=1, nums[4]=2 == nums[1]=2, i != correct_index -> duplicate found: 2
Final Answer:
Option A -> Option AQuick Check:
Duplicate 2 correctly identified [OK]
- Returning wrong duplicate due to off-by-one index
- Incrementing i too early causing missed duplicates
- Confusing value and index during swaps
Solution
Step 1: Understand problem constraints
The problem requires finding the smallest missing positive range in O(n) time and O(1) space, which excludes sorting (O(n log n)) and hash sets (O(n) space).Step 2: Identify the algorithm that achieves in-place linear time
Cyclic sort places each positive integer at its correct index in the array, enabling a single pass to find the missing range efficiently.Final Answer:
Option D -> Option DQuick Check:
Cyclic sort is known for in-place linear time sorting of numbers in range 1 to n [OK]
- Assuming sorting is optimal
- Confusing hash set space with time complexity
[3, 4, -1, 1]?Solution
Step 1: Trace the cyclic sort swaps
Initial array: [3,4,-1,1] - i=0: nums[0]=3, swap nums[2] and nums[0] -> [-1,4,3,1] - i=0 again: nums[0]=-1 (out of range), move on - i=1: nums[1]=4, swap nums[3] and nums[1] -> [-1,1,3,4] - i=1 again: nums[1]=1, swap nums[0] and nums[1] -> [1,-1,3,4] - i=1 again: nums[1]=-1 (out of range), move on - i=2 and i=3 no swaps neededStep 2: Scan for first missing positive
Array after rearrangement: [1,-1,3,4] Check indices: - i=0: nums[0]=1 (correct) - i=1: nums[1]=-1 (incorrect), so return i+1=2Final Answer:
Option A -> Option AQuick Check:
Output matches expected missing positive 2 [OK]
- Off-by-one errors in return value
- Not handling negative or out-of-range values
- Infinite loops due to missing swap condition
Solution
Step 1: Understand constraints
Duplicates and out-of-range numbers break cyclic sort assumptions; immutability prevents in-place swaps.Step 2: Choose approach handling duplicates and immutability
Hash set tracks presence without modifying input, handling duplicates and invalid numbers safely.Step 3: Analyze tradeoffs
Hash set uses O(n) space but guarantees correctness under constraints where cyclic sort fails.Final Answer:
Option C -> Option CQuick Check:
Hash set approach works with duplicates and immutable inputs [OK]
- Trying to adapt cyclic sort without handling duplicates
- Assuming sum formula always works with duplicates
- Ignoring immutability constraints
