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;45// Find missing number in array containing n-1 numbers from 1 to n6int findMissingNumber(vector<int>& arr, int n) {7 int xor1 = 0; // XOR of all numbers from 1 to n8 int xor2 = 0; // XOR of all numbers in array910 // XOR of numbers 1 to n11 for (int i = 1; i <= n; i++) {12 xor1 ^= i;13 }1415 // XOR of numbers in array16 for (int num : arr) {17 xor2 ^= num;18 }1920 // Missing number is xor1 ^ xor221 return xor1 ^ xor2;22}2324// Find two missing numbers25pair<int, int> findTwoMissingNumbers(vector<int>& arr, int n) {26 int xor1 = 0;27 int xor2 = 0;2829 // XOR of all numbers from 1 to n30 for (int i = 1; i <= n; i++) {31 xor1 ^= i;32 }3334 // XOR of numbers in array35 for (int num : arr) {36 xor2 ^= num;37 }3839 // XOR of two missing numbers40 int xorMissing = xor1 ^ xor2;4142 // Find rightmost set bit43 int rightmostSetBit = xorMissing & ~(xorMissing - 1);4445 int missing1 = 0, missing2 = 0;4647 // Separate numbers into two groups based on rightmost set bit48 for (int i = 1; i <= n; i++) {49 if (i & rightmostSetBit) {50 missing1 ^= i;51 } else {52 missing2 ^= i;53 }54 }5556 for (int num : arr) {57 if (num & rightmostSetBit) {58 missing1 ^= num;59 } else {60 missing2 ^= num;61 }62 }6364 return {missing1, missing2};65}6667int main() {68 // Test case 1: One missing number69 vector<int> arr1 = {1, 2, 4, 5, 6}; // Missing 370 int n1 = 6;71 cout << "Array: ";72 for (int num : arr1) cout << num << " ";73 cout << "\nMissing number: " << findMissingNumber(arr1, n1) << endl;7475 // Test case 2: Two missing numbers76 vector<int> arr2 = {1, 3, 5, 6}; // Missing 2 and 477 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;8283 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.