Find the Missing Number
Last Updated :
21 Sep, 2024
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 element
#include <bits/stdc++.h>
using namespace std;
int missingNumber(int n, vector<int>& arr)
{
// 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;
}
}
// Edge case handling (though problem guarantees a
// solution)
return -1;
}
/* Driver code */
int main()
{
vector<int> arr = { 1, 2, 3, 5 };
int n = 5;
cout << missingNumber(n, arr);
}
Java
import java.util.Arrays;
public class Main {
public static int missingNumber(int n, int[] arr)
{
// Create hash array of size n+1
int[] hash = new int[n + 1];
// 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;
}
}
// Edge case handling (though problem guarantees a
// solution)
return -1;
}
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 5 };
int n = 5;
System.out.println(missingNumber(n, arr));
}
}
Python
def missing_number(n, arr):
# Create hash array of size n+1
hash = [0] * (n + 1)
# Store frequencies of elements
for num in arr:
hash[num] += 1
# Find the missing number
for i in range(1, n + 1):
if hash[i] == 0:
return i
# Edge case handling (though problem guarantees a solution)
return -1
# Driver code
arr = [1, 2, 3, 5]
n = 5
print(missing_number(n, arr))
C#
using System;
public class Program {
public static int MissingNumber(int n, int[] arr)
{
// Create hash array of size n+1
int[] hash = new int[n + 1];
// 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;
}
}
// Edge case handling (though problem guarantees a
// solution)
return -1;
}
public static void Main()
{
int[] arr = { 1, 2, 3, 5 };
int n = 5;
Console.WriteLine(MissingNumber(n, arr));
}
}
JavaScript
function missingNumber(n, arr) {
// Create hash array of size n+1
const hash = new Array(n + 1).fill(0);
// Store frequencies of elements
for (let i = 0; i < n - 1; i++) {
hash[arr[i]]++;
}
// Find the missing number
for (let i = 1; i <= n; i++) {
if (hash[i] === 0) {
return i;
}
}
// Edge case handling (though problem guarantees a solution)
return -1;
}
// Driver code
const arr = [1, 2, 3, 5];
const n = 5;
console.log(missingNumber(n, arr));
Time Complexity: O(n), where n is the length of given array
Auxiliary Space: O(n)
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.
Code Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int missingNumber(int n, vector<int>& arr)
{
// 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;
}
// Driver code
int main()
{
vector<int> arr = { 1, 2, 3, 5 };
int n = 5;
cout << missingNumber(n, arr);
return 0;
}
Java
public class Main {
public static int missingNumber(int n, int[] arr)
{
int sum = 0;
// Calculate the sum of array elements
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 };
int n = 5;
System.out.println(missingNumber(n, arr));
}
}
Python
def missing_number(n, arr):
sum_arr = sum(arr)
# Calculate the expected sum
expected_sum = (n * (n + 1)) // 2
# Return the missing number
return expected_sum - sum_arr
# Driver code
arr = [1, 2, 3, 5]
n = 5
print(missing_number(n, arr))
C#
using System;
public class Program {
public static int MissingNumber(int n, int[] arr)
{
int sum = 0;
// Calculate the sum of array elements
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()
{
int[] arr = { 1, 2, 3, 5 };
int n = 5;
Console.WriteLine(MissingNumber(n, arr));
}
}
JavaScript
function missingNumber(n, arr) {
let sum = 0;
// Calculate the sum of array elements
for (let i = 0; i < n - 1; i++) {
sum += arr[i];
}
// Calculate the expected sum
const expectedSum = (n * (n + 1)) / 2;
// Return the missing number
return expectedSum - sum;
}
// Driver code
const arr = [1, 2, 3, 5];
const n = 5;
console.log(missingNumber(n, arr));
Time Complexity: O(n), where n is the length of given array
Auxiliary Space: O(1)
[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.
Code Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
// Function to get the missing number
int missingNumber(int n, vector<int>& arr)
{
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;
}
// Driver Code
int main()
{
vector<int> arr = { 1, 2, 3, 5 };
int n = 5;
// Function call
int miss = missingNumber(n, arr);
cout << miss;
return 0;
}
Java
public class Main {
public static int missingNumber(int n, int[] arr)
{
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;
}
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 5 };
int n = 5;
System.out.println(missingNumber(n, arr));
}
}
Python
def missing_number(n, arr):
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
# Driver code
arr = [1, 2, 3, 5]
n = 5
print(missing_number(n, arr))
C#
using System;
public class Program {
public static int MissingNumber(int n, int[] arr)
{
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;
}
public static void Main()
{
int[] arr = { 1, 2, 3, 5 };
int n = 5;
Console.WriteLine(MissingNumber(n, arr));
}
}
JavaScript
function missingNumber(n, arr) {
let xor1 = 0, 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;
}
// Driver code
const arr = [1, 2, 3, 5];
const n = 5;
console.log(missingNumber(n, arr));
Time Complexity: O(n), where n is the length of given array
Auxiliary Space: O(1)