Practice
findDisappearedNumbers([2,3,2,1])?Solution
Step 1: Trace swaps in the while loop
Initial array: [2,3,2,1] - i=0: nums[0]=2, correct=1; swap nums[0] and nums[1] -> [3,2,2,1] - i=0: nums[0]=3, correct=2; swap nums[0] and nums[2] -> [2,2,3,1] - i=0: nums[0]=2, correct=1; nums[0]==nums[1], so i=1 - i=1: nums[1]=2, correct=1; nums[1]==nums[1], i=2 - i=2: nums[2]=3, correct=2; nums[2]==nums[2], i=3 - i=3: nums[3]=1, correct=0; swap nums[3] and nums[0] -> [1,2,3,2] - i=3: nums[3]=2, correct=1; nums[3]==nums[1], i=4 (end)Step 2: Identify missing numbers
Final array: [1,2,3,2] Check indices: - i=0: 1 == 1 - i=1: 2 == 2 - i=2: 3 == 3 - i=3: 2 != 4 -> missing number is 4Final Answer:
Option B -> Option BQuick Check:
Only 4 is missing from 1 to 4 [OK]
- Off-by-one in missing number
- Confusing duplicates with missing
- Stopping swaps too early
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
findErrorNums([1, 2, 2, 4])?Solution
Step 1: Trace the cyclic sort swaps
Initial array: [1, 2, 2, 4]. The duplicate 2 causes a swap to place numbers correctly except index 2 which remains 2.Step 2: Identify mismatch after sorting
After sorting, array is [1, 2, 2, 4]. At index 2, expected 3 but found 2 -> duplicate=2, missing=3.Final Answer:
Option C -> Option CQuick Check:
Output matches expected duplicate and missing pair [OK]
- Confusing duplicate and missing order
- Off-by-one errors in indexing
- Returning wrong pair from loop
Solution
Step 1: Understand reuse impact
Infinite reuse means cyclic sort assumptions break because numbers can appear multiple times and be reused infinitely.Step 2: Identify correct approach
A frequency map tracks counts of each number, allowing detection of missing positives even with infinite reuse.Step 3: Why other options fail
Adding visited set won't prevent infinite swaps due to reuse. Brute force is inefficient. Priority queue adds overhead without correctness guarantee.Final Answer:
Option A -> Option AQuick Check:
Frequency map handles reuse correctly and efficiently [OK]
- Trying to patch cyclic sort with visited sets
- Assuming brute force is acceptable for large k
- Using priority queues unnecessarily
Solution
Step 1: Understand cyclic sort assumption
Cyclic sort assumes exactly one duplicate; multiple duplicates break the unique mapping to indices.Step 2: Identify solution for multiple duplicates
Using a hash set tracks seen numbers and detects any duplicate regardless of count.Step 3: Why other options fail
Continuing cyclic sort won't find all duplicates; Floyd's algorithm detects one cycle only; no modification fails on multiple duplicates.Final Answer:
Option D -> Option DQuick Check:
Hash set handles multiple duplicates correctly [OK]
- Assuming cyclic sort works unchanged for multiple duplicates
- Thinking Floyd's algorithm finds all duplicates
- Trying to modify cyclic sort without extra space
