You are given an integer array nums
representing the data collected from a survey where the numbers come from the range 1
to n
. One number is missing, and one number is duplicated.
Return an array {dup, missing}
:
dup
is the number that appears twice.missing
is the number that is missing from the array.nums
?
nums
is n
, which corresponds to the largest number that should be present.nums
?
1
to n
.To solve this problem, we’ll follow these steps:
Alternatively, we can use a more optimal approach (both time and space complexity wise) by leveraging mathematical properties:
1
to n
and the sum of squares for the range.nums
.We’ll follow this optimal mathematical approach for our solution.
#include <vector>
#include <cmath>
std::vector<int> findErrorNums(std::vector<int>& nums) {
int len = nums.size();
long long expected_sum = (len * (len + 1)) / 2;
long long expected_sum_squares = (len * (len + 1) * (2 * len + 1)) / 6;
long long actual_sum = 0;
long long actual_sum_squares = 0;
for (int num : nums) {
actual_sum += num;
actual_sum_squares += (long long)num * num;
}
long long diff_sum = expected_sum - actual_sum; // missing - duplicate
long long diff_sum_squares = expected_sum_squares - actual_sum_squares; // missing^2 - duplicate^2
long long sum_miss_dup = diff_sum_squares / diff_sum; // missing + duplicate
int missing = (diff_sum + sum_miss_dup) / 2;
int duplicate = sum_miss_dup - missing;
return {duplicate, missing};
}
expected_sum
: Sum of numbers 1
to n
.expected_sum_squares
: Sum of squares of numbers 1
to n
.actual_sum
: Sum of the given array nums
.actual_sum_squares
: Sum of squares of the numbers in nums
.diff_sum
: This yields missing - duplicate
.diff_sum_squares
: This yields missing^2 - duplicate^2
.missing + duplicate = diff_sum_squares / diff_sum
.missing
and duplicate
.O(n)
because we only make a single pass through the nums
array.O(1)
as we only use a constant amount of extra space for calculation purposes.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?