2965. Find Missing and Repeated Values - Detailed Explanation
Problem Statement:
You are given an array of ( n ) integers, where the integers are in the range ([1, n]). In this array, exactly one number appears twice (the repeated value) and exactly one number is missing (the missing value). Your task is to find the repeated number and the missing number and return them (typically as a pair or an array).
For example, if the array represents a set (\{1, 2, \ldots, n\}) with one error (one number occurs twice and one number is missing), determine which number is repeated and which number is missing.
Example Inputs and Outputs:
-
Example 1:
- Input:
nums = [1, 2, 2, 4]
- Output:
[2, 3]
- Explanation:
The number2
appears twice, and the number3
is missing.
- Input:
-
Example 2:
- Input:
nums = [3, 1, 2, 5, 3]
- Output:
[3, 4]
- Explanation:
The number3
is repeated, and the number4
is missing.
- Input:
-
Example 3:
- Input:
nums = [1, 1]
- Output:
[1, 2]
- Explanation:
In an array of size 2, the numbers should be ([1, 2]). Here,1
is repeated and2
is missing.
- Input:
Constraints:
- (1 \leq n \leq 10^5) (the size of the array)
- The array elements are in the range ([1, n]).
- Exactly one value is repeated and exactly one value is missing.
Hints for Solving the Problem:
-
Sorting Insight:
- How can sorting the array help you quickly identify when a duplicate occurs and when a gap (missing number) is present?
-
Counting with a Hash Table:
- By using a hash map (or dictionary), you can count the frequency of each number. Which number appears twice, and which number doesn’t appear at all?
-
Mathematical Relationships:
- The expected sum of numbers from (1) to (n) is (S = frac{n(n+1)}{2}) and the expected sum of squares is (S_2 = \frac{n(n+1)(2n+1)}{6}). How can you use the differences between the actual and expected sums (and sum of squares) to derive equations that yield the repeated and missing numbers?
Approach 1: Sorting-Based Method
Step-by-Step Explanation:
-
Sort the Array:
- Sort the array in non-decreasing order.
-
Scan Through the Sorted Array:
-
As you iterate over the sorted array, look for the duplicate (i.e. when ( \text{nums}[i] == \text{nums}[i+1] )).
-
Also check the gap between consecutive elements. If the difference is more than 1, the missing number lies in the gap.
-
Additionally, check the boundaries: if the first element is not 1 or the last element is not ( n ).
-
-
Return the Result:
- Once you identify both the repeated and the missing numbers, return them.
Python Code (Sorting Approach):
Java Code (Sorting Approach):
Approach 2: Hash Table (Counting) Method
Step-by-Step Explanation:
-
Count Occurrences:
- Traverse the array and count the frequency of each number using a hash map (or dictionary).
-
Identify the Error:
- The number that appears twice (frequency 2) is the duplicate.
- The number that does not appear (frequency 0) in the range ([1, n]) is the missing number.
Python Code (Hash Table Approach):
Java Code (Hash Table Approach):
Approach 3: Mathematical Method
Step-by-Step Explanation:
-
Compute Expected Sums:
- The expected sum of numbers from (1) to (n) is:
[ S = \frac{n(n+1)}{2} ] - The expected sum of squares is:
[ S_2 = \frac{n(n+1)(2n+1)}{6} ]
- The expected sum of numbers from (1) to (n) is:
-
Compute Actual Sums:
- Let (S_{actual}) be the sum of the given array and (S_{2,actual}) be the sum of squares of the given array.
-
Set Up Equations:
- Let (A) be the repeated number and (B) be the missing number. Then:
[ S_{actual} - S = A - B ] and
[ S_{2,actual} - S_2 = A^2 - B^2 = (A - B)(A + B) ] - With these two equations, solve for (A) and (B).
- Let (A) be the repeated number and (B) be the missing number. Then:
Python Code (Mathematical Approach):
Java Code (Mathematical Approach):
Complexity Analysis:
-
Sorting Approach:
- Time Complexity: (O(n \log n)) due to sorting.
- Space Complexity: (O(1)) if sorting is done in-place.
-
Hash Table Approach:
- Time Complexity: (O(n))
- Space Complexity: (O(n))
-
Mathematical Approach:
- Time Complexity: (O(n))
- Space Complexity: (O(1))
Edge Cases:
-
Single Element Array:
- For an array like ([x]) (with (n = 1)), the only valid output would be ([x, 2]) if (x \neq 1); however, by problem definition, (n \geq 2).
-
All Zeros or Invalid Input:
- Input always contains numbers in the range ([1, n]) with exactly one duplicate and one missing number. Deviations are not expected per constraints.
Common Mistakes:
-
Off-by-One Errors:
- When iterating over the range (1) to (n), ensure the boundaries are correctly handled.
-
Incorrect Frequency Counting:
- In the hash table approach, miscounting frequencies may lead to wrong identification of the duplicate or missing number.
-
Integer Overflow:
- In the mathematical approach, using a proper data type (like
long
in Java) is crucial when summing large numbers or squares.
- In the mathematical approach, using a proper data type (like
Alternative Variations:
- Multiple Duplicates/Missing Values:
- Variants of the problem may involve more than one duplicate or more than one missing value, requiring modified logic.
- Finding Only the Duplicate or Only the Missing Number:
- Some problems focus solely on finding one of the two values, which may simplify the approach.
Related Problems for Further Practice:
- Set Mismatch (LeetCode 645):
- A similar problem asking to find the duplicate and missing number.
- Find All Numbers Disappeared in an Array (LeetCode 448):
- Identify all numbers missing from an array.
- Missing Number (LeetCode 268):
- Find the single missing number when no duplicate exists.
GET YOUR FREE
Coding Questions Catalog
