Find Missing Number using XOR in C++

Find Missing Number in Array using XOR in C++.

AdvancedBitwise Operations ProgramsExample 5 of 5
find-missing-number-using-xor.cpp
1#include <iostream>
2#include <vector>
3using namespace std;
4
5// Find missing number in array containing n-1 numbers from 1 to n
6int findMissingNumber(vector<int>& arr, int n) {
7 int xor1 = 0; // XOR of all numbers from 1 to n
8 int xor2 = 0; // XOR of all numbers in array
9
10 // XOR of numbers 1 to n
11 for (int i = 1; i <= n; i++) {
12 xor1 ^= i;
13 }
14
15 // XOR of numbers in array
16 for (int num : arr) {
17 xor2 ^= num;
18 }
19
20 // Missing number is xor1 ^ xor2
21 return xor1 ^ xor2;
22}
23
24// Find two missing numbers
25pair<int, int> findTwoMissingNumbers(vector<int>& arr, int n) {
26 int xor1 = 0;
27 int xor2 = 0;
28
29 // XOR of all numbers from 1 to n
30 for (int i = 1; i <= n; i++) {
31 xor1 ^= i;
32 }
33
34 // XOR of numbers in array
35 for (int num : arr) {
36 xor2 ^= num;
37 }
38
39 // XOR of two missing numbers
40 int xorMissing = xor1 ^ xor2;
41
42 // Find rightmost set bit
43 int rightmostSetBit = xorMissing & ~(xorMissing - 1);
44
45 int missing1 = 0, missing2 = 0;
46
47 // Separate numbers into two groups based on rightmost set bit
48 for (int i = 1; i <= n; i++) {
49 if (i & rightmostSetBit) {
50 missing1 ^= i;
51 } else {
52 missing2 ^= i;
53 }
54 }
55
56 for (int num : arr) {
57 if (num & rightmostSetBit) {
58 missing1 ^= num;
59 } else {
60 missing2 ^= num;
61 }
62 }
63
64 return {missing1, missing2};
65}
66
67int main() {
68 // Test case 1: One missing number
69 vector<int> arr1 = {1, 2, 4, 5, 6}; // Missing 3
70 int n1 = 6;
71 cout << "Array: ";
72 for (int num : arr1) cout << num << " ";
73 cout << "\nMissing number: " << findMissingNumber(arr1, n1) << endl;
74
75 // Test case 2: Two missing numbers
76 vector<int> arr2 = {1, 3, 5, 6}; // Missing 2 and 4
77 int n2 = 6;
78 cout << "\nArray: ";
79 for (int num : arr2) cout << num << " ";
80 auto result = findTwoMissingNumbers(arr2, n2);
81 cout << "\nMissing numbers: " << result.first << " and " << result.second << endl;
82
83 return 0;
84}

Output

Array: 1 2 4 5 6
Missing number: 3

Array: 1 3 5 6
Missing numbers: 2 and 4

What's going on

The program demonstrates finding missing numbers using XOR:

Finding single missing number
Finding two missing numbers
Using XOR properties efficiently
O(n) time, O(1) space solution

XOR approach provides optimal solution for missing number problems.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <vector>
Provides vector container class.

Understanding XOR Properties

Key XOR Properties

:

a ^ a = 0 (XOR with itself is zero)
a ^ 0 = a (XOR with zero is identity)
XOR is commutative: a ^ b = b ^ a
XOR is associative: (a ^ b) ^ c = a ^ (b ^ c)

Why Useful

:

XOR cancels out pairs
Missing number appears once
Other numbers appear twice (cancel)
Result is missing number

Finding Single Missing Number

Algorithm

:

1.XOR all numbers from 1 to n → xor1
2.XOR all numbers in array → xor2
3.Missing number = xor1 ^ xor2

How it works

:

xor1: XOR of complete set (1 to n)
xor2: XOR of array (missing one number)
xor1 ^ xor2: cancels all pairs, leaves missing
Example: {1,2,4,5} missing 3
xor1 = 1^2^3^4^5
xor2 = 1^2^4^5
Result = 3

Finding Two Missing Numbers

Algorithm

:

1.XOR all numbers 1 to n → xor1
2.XOR all numbers in array → xor2
3.xorMissing = xor1 ^ xor2 (XOR of two missing)
4.Find rightmost set bit in xorMissing
5.Separate numbers into two groups
6.Find missing in each group

How it works

:

xorMissing contains XOR of two missing numbers
Rightmost set bit: numbers differ at this bit
Group by this bit: one missing in each group
Find missing in each group separately

Rightmost Set Bit

Finding Rightmost Set Bit

:

int rightmostSetBit = xorMissing & ~(xorMissing - 1);

How it works

:

xorMissing - 1: flips rightmost set bit and bits to right
~(xorMissing - 1): inverts (all 1s except rightmost set bit)
xorMissing & ~(...): extracts rightmost set bit
Isolates the differing bit position

Separating into Groups

Grouping Logic

:

Group 1: numbers with rightmost set bit set
Group 2: numbers with rightmost set bit clear
One missing number in each group
Find missing in each group using XOR

How it works

:

Two missing numbers differ at rightmost set bit
One has bit set, other has bit clear
Separates them into different groups
Each group has one missing number

Time and Space Complexity

Time Complexity

:

O(n) - single pass through numbers
Linear time complexity
Optimal for this problem
Very efficient

Space Complexity

:

O(1) - constant extra space
Only few variables needed
No additional data structures
Space optimal

When to Use XOR Approach

Best For

:

Finding missing numbers
Array with one missing
Array with two missing
Space-constrained problems
Efficient solutions needed

Example Scenarios

:

Finding missing ID
Array with missing element
Duplicate detection
Efficient algorithms
Interview problems

Important Considerations

XOR Properties

:

Understand XOR behavior
Cancellation property
Pair elimination
Foundation of algorithm

Edge Cases

:

Handle empty array
Single element missing
Two elements missing
Boundary conditions

Efficiency

:

Optimal time: O(n)
Optimal space: O(1)
Better than sorting
Better than hash set

return 0;

This ends the program successfully.