#include<vector>#include<cmath>classSolution {
public: std::vector<int> findMissingTwo(const std::vector<int>& nums, int n) {
longlong S = (longlong)n * (n +1) /2;
longlong S1 =0, P1 =1, P =1;
for (int x : nums) {
S1 += x;
P1 *= x;
}
for (int i =1; i <= n; ++i) P *= i;
longlong s = S - S1;
longlong p = P / P1;
longlong d = (longlong)std::sqrt(s * s -4* p);
int x = (int)((s + d) /2);
int y = (int)(s - x);
return {x, y};
}
};
classSolution {
publicint[]findMissingTwo(int[] nums, int n) {
long S = (long)n * (n + 1) / 2;
long S1 = 0, P1 = 1, P = 1;
for (int x : nums) {
S1 += x;
P1 *= x;
}
for (int i = 1; i <= n; i++) P *= i;
long s = S - S1;
long p = P / P1;
long d = (long)Math.sqrt(s * s - 4 * p);
int x = (int)((s + d) / 2);
int y = (int)(s - x);
returnnewint[]{x, y};
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import math
classSolution:
deffindMissingTwo(self, nums: list[int], n: int) -> list[int]:
S = n * (n +1) //2 S1 = sum(nums)
P = math.prod(range(1, n+1))
P1 =1for x in nums:
P1 *= x
s = S - S1
p = P // P1
# x^2 - s*x + p = 0 d = int((s**2-4*p)**0.5)
x = (s + d) //2 y = s - x
return [x, y]
Method 2 – In-place Marking (Negation, O(n) time, O(1) space, but mutates input)#
This approach avoids multiplication and uses the fact that the numbers are positive and in the range [1, n].
We use each element of the array nums as an index to mark presence. For each value nums[i], we mark the element at index abs(nums[i]) - 1 as negative. After marking, the indices that remain positive correspond to the missing numbers.
A subtlety: If we simply overwrite values (e.g., setting to 0), we lose information. By negating the value at the index, we preserve the original value (up to sign) and can later restore the array if needed. For example, if nums = [2, 1, 5, 8, 6, 7, 3, 9], after marking, the positive entries indicate the missing numbers.
After marking, a second pass over nums finds all indices with positive values—these are the missing numbers (add 1 to the index to get the actual number). Optionally, you can restore the array by negating all negative values back to positive.
classSolution {
publicint[]findMissingTwo(int[] nums, int n) {
int[] arr = nums.clone();
for (int x : arr) {
int idx = Math.abs(x) - 1;
if (idx < n) arr[idx]=-Math.abs(arr[idx]);
}
int[] res =newint[2];
int j = 0;
for (int i = 0; i < n; i++) {
if (arr[i]> 0) res[j++]= i + 1;
}
return res;
}
}
1
2
3
4
5
6
7
8
classSolution:
deffindMissingTwo(self, nums: list[int], n: int) -> list[int]:
arr = nums[:]
for x in arr:
idx = abs(x) -1if idx < n:
arr[idx] =-abs(arr[idx])
return [i+1for i in range(n) if arr[i] >0]
The above solution assumes that the input array is mutable. But what if nums is immutable and we still need to find the missing values in O(n) time and constant space? Here, we use the properties of XOR.
If there were no missing numbers, then the XOR of all numbers from 1 to n (let’s call it xor1 = 1 ^ 2 ^ ... ^ n) and the XOR of all numbers in the array (xor2 = nums[0] ^ nums[1] ^ ... ^ nums[n-3]) would be equal, so their XOR would be 0.
With two missing numbers, the XOR of all numbers from 1 to n and all elements in nums gives xor = p ^ q, where p and q are the missing numbers. All bits set in xor are set in either p or q.
To separate p and q, pick any set bit in xor (commonly the rightmost set bit, which can be found as mask = xor & -xor). Partition all numbers from 1 to n and all elements in nums into two groups based on this bit, and XOR within each group. This will isolate p in one group and q in the other.
This approach works in O(n) time and O(1) space, and does not modify the input array.
classSolution:
deffindMissingTwo(self, nums: list[int], n: int) -> list[int]:
xor =0for x in nums:
xor ^= x
for i in range(1, n+1):
xor ^= i
# Find rightmost set bit mask = xor &-xor
a = b =0for x in nums:
if x & mask:
a ^= x
else:
b ^= x
for i in range(1, n+1):
if i & mask:
a ^= i
else:
b ^= i
return [a, b]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
publicint[]findMissingTwo(int[] nums, int n) {
int xor = 0;
for (int x : nums) xor ^= x;
for (int i = 1; i <= n; i++) xor ^= i;
int mask = xor &-xor;
int a = 0, b = 0;
for (int x : nums) {
if ((x & mask) != 0) a ^= x;
else b ^= x;
}
for (int i = 1; i <= n; i++) {
if ((i & mask) != 0) a ^= i;
else b ^= i;
}
returnnewint[]{a, b};
}
}
#include<vector>classSolution {
public: std::vector<int> findMissingTwo(const std::vector<int>& nums, int n) {
int xorv =0;
for (int x : nums) xorv ^= x;
for (int i =1; i <= n; ++i) xorv ^= i;
int mask = xorv &-xorv;
int a =0, b =0;
for (int x : nums) {
if (x & mask) a ^= x;
else b ^= x;
}
for (int i =1; i <= n; ++i) {
if (i & mask) a ^= i;
else b ^= i;
}
return {a, b};
}
};