Given an array arr[] of size n-1 with integers in the range of [1, n], the task is to find the missing number from the first n integers.
Note: There are no duplicates in the list.
Examples:
Input: arr[] = [1, 2, 4, 6, 3, 7, 8] , n = 8
Output: 5
Explanation: Here the size of the array is 8, so the range will be [1, 8]. The missing number between 1 to 8 is 5
Input: arr[] = [1, 2, 3, 5], n = 5
Output: 4
Explanation: Here the size of the array is 4, so the range will be [1, 5]. The missing number between 1 to 5 is 4
[Naive Approach] Using Hashing – O(n) time and O(n) auxiliary space
The very basic idea is to use an array to store the frequency of each element in the given array. The number with a frequency of 0 is the missing number.
C++
// C++ program to find the missing number
#include <iostream>
#include <vector>
using namespace std;
int missingNumber(vector<int> &arr) {
int n = arr.size() + 1;
// Create hash array of size n+1
vector<int> hash(n + 1, 0);
// Store frequencies of elements
for (int i = 0; i < n - 1; i++) {
hash[arr[i]]++;
}
// Find the missing number
for (int i = 1; i <= n; i++) {
if (hash[i] == 0) {
return i;
}
}
return -1;
}
int main() {
vector<int> arr = {1, 2, 3, 5};
int res = missingNumber(arr);
cout << res << endl;
return 0;
}
Java
//java program to find the missing number
class GfG {
static int missingNumber(int[] arr) {
int n = arr.length + 1;
// Create hash array of size n+1
int[] hash = new int[n + 1];
// Store frequencies of elements
for (int i = 0; i < arr.length; i++) {
hash[arr[i]]++;
}
// Find the missing number
for (int i = 1; i <= n; i++) {
if (hash[i] == 0) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr
= { 1, 2, 3, 5 };
int res = missingNumber(arr);
System.out.println(res);
}
}
Python
# Python program to find the missing number
def missingNumber(arr):
n = len(arr) + 1
# Create hash array of size n+1
hashArr = [0] * (n + 1)
# Store frequencies of elements
for num in arr:
hashArr[num] += 1
# Find the missing number
for i in range(1, n + 1):
if hashArr[i] == 0:
return i
return -1
arr = [1, 2, 3, 5]
result = missingNumber(arr)
print(result)
C#
// C# program to find the missing number
using System;
class GfG {
static int missingNumber(int[] arr) {
int n = arr.Length + 1;
// Create hash array of size n+1
int[] hashArr = new int[n + 1];
// Store frequencies of elements
foreach(int num in arr) { hashArr[num]++; }
// Find the missing number
for (int i = 1; i <= n; i++) {
if (hashArr[i] == 0) {
return i;
}
}
return -1;
}
static void Main() {
int[] arr = { 1, 2, 3, 5 };
int res = missingNumber(arr);
Console.WriteLine(res);
}
}
JavaScript
// JavaScript program to find the missing number
function missingNumber(arr) {
const n
= arr.length
+ 1;
// Create hash array of size n+1
const hashArr = new Array(n + 1).fill(0);
// Store frequencies of elements
for (let i = 0; i < arr.length; i++) {
hashArr[arr[i]]++;
}
// Find the missing number
for (let i = 1; i <= n; i++) {
if (hashArr[i] === 0) {
return i;
}
}
return -1;
}
const arr = [ 1, 2, 3, 5 ];
const res = missingNumber(arr);
console.log(res);
An efficient approach is to use summation formula. As we know that the sum of the first n natural numbers is given by the formula n * (n + 1) / 2. Compute this sum and subtract the sum of all elements in the array from it to get the missing number.
Detailed Explanation:
- Compute the sum of the first n natural numbers as expected sum using the formula.
- Calculate the sum of the given array elements.
- Subtract the sum of the array elements from the expected sum to find the missing number.
C++
// C++ program to find the missing number
#include <bits/stdc++.h>
using namespace std;
int missingNumber(vector<int> &arr) {
int n = arr.size() + 1;
// Calculate the sum of array elements
int sum = 0;
for (int i = 0; i < n - 1; i++) {
sum += arr[i];
}
// Calculate the expected sum
int expectedSum = (n * (n + 1)) / 2;
// Return the missing number
return expectedSum - sum;
}
int main() {
vector<int> arr = {1, 2, 3, 5};
cout << missingNumber(arr);
return 0;
}
Java
// Java program to find the missing number
class GfG {
static int missingNumber(int[] arr) {
int n = arr.length + 1;
// Calculate the sum of array elements
int sum = 0;
for (int i = 0; i < n - 1; i++) {
sum += arr[i];
}
// Calculate the expected sum
int expectedSum = (n * (n + 1)) / 2;
// Return the missing number
return expectedSum - sum;
}
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 5 };
System.out.println(missingNumber(arr));
}
}
Python
# Python program to find the missing number
def missingNumber(arr):
n = len(arr) + 1
# Calculate the sum of array elements
totalSum = sum(arr)
# Calculate the expected sum
expectedSum = (n * (n + 1)) // 2
# Return the missing number
return expectedSum - totalSum
arr = [1, 2, 3, 5]
print(missingNumber(arr))
C#
// C# program to find the missing number
using System;
class GfG {
static int missingNumber(int[] arr) {
int n = arr.Length + 1;
// Calculate the sum of array elements
int totalSum = 0;
foreach (int num in arr) {
totalSum += num;
}
// Calculate the expected sum
int expectedSum = (n * (n + 1)) / 2;
// Return the missing number
return expectedSum - totalSum;
}
static void Main() {
int[] arr = { 1, 2, 3, 5 };
Console.WriteLine(missingNumber(arr));
}
}
JavaScript
// Javascript program to find the missing number
function missingNumber(arr) {
let n = arr.length + 1;
// Calculate the sum of array elements
let totalSum = 0;
for (let i = 0; i < arr.length; i++) {
totalSum += arr[i];
}
// Calculate the expected sum
let expectedSum = (n * (n + 1)) / 2;
// Return the missing number
return expectedSum - totalSum;
}
const arr = [1, 2, 3, 5];
console.log(missingNumber(arr));
[Expected Approach-2] Using XOR Operation – O(n) time and O(1) auxiliary space
Another efficient approach is to use the XOR operation. The approach is based on the idea that XOR of a number with itself is 0, and XOR of a number with 0 is the number itself. This means that if we find XOR of first N natural numbers and then take XOR of each array elements with this, then the resultant will be the missing number.
Using these properties, the approach can be understood as::
- First find XOR all numbers in the range
[1, n] - Then XOR all elements of the array with the XOR(1, n).
- The result will be the missing number.
C++
// C++ program to find the missing number
#include <bits/stdc++.h>
using namespace std;
int missingNumber( vector<int>& arr) {
int n= arr.size()+1;
int xor1 = 0, xor2 = 0;
// XOR all array elements
for (int i = 0; i < n - 1; i++) {
xor2 ^= arr[i];
}
// XOR all numbers from 1 to n
for (int i = 1; i <= n; i++) {
xor1 ^= i;
}
// Missing number is the XOR of xor1 and xor2
return xor1 ^ xor2;
}
int main() {
vector<int> arr = { 1, 2, 3, 5 };
int n = 5;
int res = missingNumber( arr);
cout << res;
return 0;
}
Java
// Java program to find the missing number
class GfG {
static int missingNumber( int[] arr) {
int xor1 = 0, xor2 = 0;
int n= arr.length+1;
// XOR all array elements
for (int i = 0; i < n - 1; i++) {
xor2 ^= arr[i];
}
// XOR all numbers from 1 to n
for (int i = 1; i <= n; i++) {
xor1 ^= i;
}
// Missing number is the XOR of xor1 and xor2
return xor1 ^ xor2;
}
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 5 };
int n = 5;
System.out.println(missingNumber(arr));
}
}
Python
# Python program to find the missing number
def missingNumber(arr):
n = len(arr) + 1
xor1 = 0
xor2 = 0
# XOR all array elements
for num in arr:
xor2 ^= num
# XOR all numbers from 1 to n
for i in range(1, n + 1):
xor1 ^= i
# Missing number is the XOR of xor1 and xor2
return xor1 ^ xor2
arr = [1, 2, 3, 5]
res = missingNumber(arr)
print(res)
C#
// C# program to find the missing number
using System;
class GfG {
static int missingNumber(int[] arr) {
int n = arr.Length + 1;
int xor1 = 0;
int xor2 = 0;
// XOR all array elements
foreach (var num in arr) {
xor2 ^= num;
}
// XOR all numbers from 1 to n
for (int i = 1; i <= n; i++) {
xor1 ^= i;
}
// Missing number is the XOR of xor1 and xor2
return xor1 ^ xor2;
}
static void Main(string[] args) {
int[] arr = { 1, 2, 3, 5 };
int res = missingNumber(arr);
Console.WriteLine(res);
}
}
JavaScript
// JavaScript program to find the missing number
function missingNumber(arr) {
const n = arr.length + 1;
let xor1 = 0;
let xor2 = 0;
// XOR all array elements
for (let i = 0; i < n - 1; i++) {
xor2 ^= arr[i];
}
// XOR all numbers from 1 to n
for (let i = 1; i <= n; i++) {
xor1 ^= i;
}
// Missing number is the XOR of xor1 and xor2
return xor1 ^ xor2;
}
const arr = [ 1, 2, 3, 5 ];
console.log(missingNumber(arr));