[Easy] 645. Set Mismatch
You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.
You are given an integer array nums representing the data status of this set after the error.
Find the number that occurs twice and the number that is missing and return them in the form [duplicate, missing].
Examples
Example 1:
Input: nums = [1,2,2,4]
Output: [2,3]
Explanation: 2 is duplicated, 3 is missing.
Example 2:
Input: nums = [1,1]
Output: [1,2]
Explanation: 1 is duplicated, 2 is missing.
Constraints
2 <= nums.length <= 10^41 <= nums[i] <= 10^4- Exactly one number appears twice, exactly one is missing.
Thinking Process
- Mathematical Approach: Elegant solution using sum and square sum differences
- Clarify if the array is sorted, has negatives, or allows duplicates.
- Prefix sums answer range queries; hash maps answer pair/count queries.
- In-place tricks use swap/write index instead of extra arrays.
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| Prefix sum (this problem) | O(n) | O(n) | Range queries, subarray sum |
| Sort + scan | O(n log n) | O(1) | Intervals, meeting rooms |
| Kadane’s algorithm | O(n) | O(1) | Maximum subarray |
| Hash map counting | O(n) | O(n) | Frequency, two-sum variants |
Solution
class Solution:
def findErrorNums(self, nums):
N = len(nums)
x = 0
y = 0
for i in range(1, N + 1):
x += nums[i - 1] - i
y += nums[i - 1] * nums[i - 1] - i * i
missing = (y // x + x) // 2
duplicate = missing + x
return [duplicate, missing]
Solution Explanation
Approach: Prefix sum (this problem)
Key idea: 1. Mathematical Approach: Elegant solution using sum and square sum differences
How the code works:
- Mathematical Approach: Elegant solution using sum and square sum differences
- Clarify if the array is sorted, has negatives, or allows duplicates.
- Prefix sums answer range queries; hash maps answer pair/count queries.
- In-place tricks use swap/write index instead of extra arrays.
Walkthrough — input nums = [1,2,2,4], expected output [2,3]:
2 is duplicated, 3 is missing.
Common Mistakes
- Smallest input:
nums = [1,1]→[1,2] - Largest input:
nums = [1,2,3,...,n-1,n,n]→[n, n+1](but n+1 > n, so invalid) - Duplicate at start:
nums = [2,2,3,4]→[2,1] - Duplicate at end:
nums = [1,2,3,3]→[3,4](but 4 > 3, so invalid) - Missing at start:
nums = [2,2,3,4]→[2,1] -
Missing at end:
nums = [1,1,2,3]→[1,4](but 4 > 3, so invalid) - Integer overflow: Not using
long longfor square calculations - Index off-by-one: Confusing 0-based vs 1-based indexing
- Sign errors: Incorrect handling of negative values in mathematical approach
- Division by zero: Not checking if
x == 0(shouldn’t happen per constraints) - Wrong order: Returning
[missing, duplicate]instead of[duplicate, missing]
Related Problems
- LC 41: First Missing Positive - Find missing positive number
- LC 268: Missing Number - Find single missing number
- LC 442: Find All Duplicates in an Array - Find all duplicates
- LC 448: Find All Numbers Disappeared in an Array - Find all missing numbers
- LC 287: Find the Duplicate Number - Find duplicate (no missing)
Key Takeaways
- Mathematical Approach: Elegant solution using sum and square sum differences
- Negative Marking: Space-efficient in-place solution
- Hash Map: Simple and intuitive, uses extra space
- XOR: Bit manipulation approach, more complex but interesting
References
- LC 645: Set Mismatch on LeetCode
- LeetCode Discuss — LC 645: Set Mismatch
- LeetCode Editorial (may require premium)