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
k. You need to find the kth positive integer that is missing from this array. Which algorithmic approach guarantees the most efficient solution in terms of time complexity?Solution
Step 1: Understand the problem constraints
The array is sorted and contains unique positive integers. We want the kth missing positive number not in the array.Step 2: Identify the optimal approach
Binary search can be used to find the smallest index where the count of missing numbers is at least k, leveraging the formula missing = arr[mid] - (mid + 1). This yields O(log n) time complexity.Final Answer:
Option A -> Option AQuick Check:
Binary search exploits sorted order and missing count formula [OK]
- Thinking brute force is efficient enough
- Confusing DP with missing count calculation
Solution
Step 1: Inspect duplicate appending loop
Duplicates were appended twice for each mismatch, causing repeated entries. The bug is fixed by removing the repeated append.Step 2: Confirm other lines are correct
Index calculation and swap condition are correct; incrementing i is proper to avoid infinite loop.Final Answer:
Option A -> Option AQuick Check:
Duplicate elements appear twice in output due to repeated append [OK]
- Infinite loop due to missing i increment
- Off-by-one errors in index calculation
- Incorrect swap condition reversing logic
Solution
Step 1: Understand input constraints
Input may have invalid numbers (negatives, zeros, > n) and duplicates that break in-place cyclic sort.Step 2: Use extra array for placement
Creating a separate array of size n and placing valid numbers at index num-1 ensures correct placement ignoring invalids and duplicates.Step 3: Avoid in-place swaps that cause index errors or infinite loops
Extra space approach is safer and simpler for this variant.Final Answer:
Option B -> Option BQuick Check:
Extra array handles invalid and duplicate numbers cleanly [OK]
- Ignoring invalid range checks
- Trying to fix in-place swaps with invalid numbers
- Not handling duplicates properly
Solution
Step 1: Understand impact of duplicates
Duplicates break the assumption that arr[i] - (i + 1) equals missing count because indices no longer correspond to unique elements.Step 2: Identify correct fix
Removing duplicates restores the strictly increasing property needed for binary search and correct missing count calculation.Final Answer:
Option C -> Option CQuick Check:
Preprocessing to unique elements preserves binary search correctness [OK]
- Assuming duplicates don't affect missing count
- Trying to fix missing count formula without removing duplicates
