Find Missing Number using XOR
Find Missing Number in Array using XOR in C++
C++ Find Missing Number using XOR Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <vector>
using namespace std;
// Find missing number in array containing n-1 numbers from 1 to n
int findMissingNumber(vector<int>& arr, int n) {
int xor1 = 0; // XOR of all numbers from 1 to n
int xor2 = 0; // XOR of all numbers in array
// XOR of numbers 1 to n
for (int i = 1; i <= n; i++) {
xor1 ^= i;
}
// XOR of numbers in array
for (int num : arr) {
xor2 ^= num;
}
// Missing number is xor1 ^ xor2
return xor1 ^ xor2;
}
// Find two missing numbers
pair<int, int> findTwoMissingNumbers(vector<int>& arr, int n) {
int xor1 = 0;
int xor2 = 0;
// XOR of all numbers from 1 to n
for (int i = 1; i <= n; i++) {
xor1 ^= i;
}
// XOR of numbers in array
for (int num : arr) {
xor2 ^= num;
}
// XOR of two missing numbers
int xorMissing = xor1 ^ xor2;
// Find rightmost set bit
int rightmostSetBit = xorMissing & ~(xorMissing - 1);
int missing1 = 0, missing2 = 0;
// Separate numbers into two groups based on rightmost set bit
for (int i = 1; i <= n; i++) {
if (i & rightmostSetBit) {
missing1 ^= i;
} else {
missing2 ^= i;
}
}
for (int num : arr) {
if (num & rightmostSetBit) {
missing1 ^= num;
} else {
missing2 ^= num;
}
}
return {missing1, missing2};
}
int main() {
// Test case 1: One missing number
vector<int> arr1 = {1, 2, 4, 5, 6}; // Missing 3
int n1 = 6;
cout << "Array: ";
for (int num : arr1) cout << num << " ";
cout << "\nMissing number: " << findMissingNumber(arr1, n1) << endl;
// Test case 2: Two missing numbers
vector<int> arr2 = {1, 3, 5, 6}; // Missing 2 and 4
int n2 = 6;
cout << "\nArray: ";
for (int num : arr2) cout << num << " ";
auto result = findTwoMissingNumbers(arr2, n2);
cout << "\nMissing numbers: " << result.first << " and " << result.second << endl;
return 0;
}Array: 1 2 4 5 6 Missing number: 3 Array: 1 3 5 6 Missing numbers: 2 and 4
Understanding Find Missing Number using XOR
This program teaches you how to Find Missing Number in Array using XOR in C++. This is an elegant algorithm that uses XOR properties to efficiently find missing numbers without using extra space. The XOR approach is optimal for this problem, providing O(n) time and O(1) space complexity.
---
1. What This Program Does
The program demonstrates finding missing numbers using XOR:
XOR approach provides optimal solution for missing number problems.
---
2. Header Files Used
---
3. Understanding XOR Properties
Key XOR Properties
:
Why Useful
:
---
4. Finding Single Missing Number
Algorithm
:
How it works
:
---
5. Finding Two Missing Numbers
Algorithm
:
How it works
:
---
6. Rightmost Set Bit
Finding Rightmost Set Bit
:
int rightmostSetBit = xorMissing & ~(xorMissing - 1);
How it works
:
---
7. Separating into Groups
Grouping Logic
:
How it works
:
---
8. Time and Space Complexity
Time Complexity
:
Space Complexity
:
---
9. When to Use XOR Approach
Best For
:
Example Scenarios
:
---
10. Important Considerations
XOR Properties
:
Edge Cases
:
Efficiency
:
---
11. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning XOR algorithms, understanding efficient problem-solving, and preparing for advanced array manipulation problems in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Find Missing Number using XOR
This C++ program is part of the "Bitwise Operations Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.