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: The missing number between 1 to 8 is 5
Input: arr[] = {1, 2, 3, 5}, N = 5
Output: 4
Explanation: The missing number between 1 to 5 is 4
Approach 1 (Using Hashing): The idea behind the following approach is
The numbers will be in the range (1, N), an array of size N can be maintained to keep record of the elements present in the given array
- Create a temp array temp[] of size n + 1 with all initial values as 0.
- Traverse the input array arr[], and do following for each arr[i]
- if(temp[arr[i]] == 0) temp[arr[i]] = 1
- Traverse temp[] and output the array element having value as 0 (This is the missing element).
Below is the implementation of the above approach:
C
#include <stdio.h>
void findMissing( int arr[], int N)
{
int temp[N + 1];
for ( int i = 0; i <= N; i++) {
temp[i] = 0;
}
for ( int i = 0; i < N; i++) {
temp[arr[i] - 1] = 1;
}
int ans;
for ( int i = 0; i <= N; i++) {
if (temp[i] == 0)
ans = i + 1;
}
printf ( "%d" , ans);
}
int main()
{
int arr[] = { 1, 3, 7, 5, 6, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
findMissing(arr, n);
}
|
C++
#include <bits/stdc++.h>
using namespace std;
void findMissing( int arr[], int N)
{
int i;
int temp[N + 1];
for ( int i = 0; i <= N; i++){
temp[i] = 0;
}
for (i = 0; i < N; i++){
temp[arr[i] - 1] = 1;
}
int ans;
for (i = 0; i <= N ; i++) {
if (temp[i] == 0)
ans = i + 1;
}
cout << ans;
}
int main()
{
int arr[] = { 1, 3, 7, 5, 6, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
findMissing(arr, n);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void findMissing( int arr[], int N)
{
int i;
int temp[] = new int [N + 1 ];
for (i = 0 ; i <= N; i++) {
temp[i] = 0 ;
}
for (i = 0 ; i < N; i++) {
temp[arr[i] - 1 ] = 1 ;
}
int ans = 0 ;
for (i = 0 ; i <= N; i++) {
if (temp[i] == 0 )
ans = i + 1 ;
}
System.out.println(ans);
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 7 , 5 , 6 , 2 };
int n = arr.length;
findMissing(arr, n);
}
}
|
Python3
def findMissing(arr, N):
temp = [ 0 ] * (N + 1 )
for i in range ( 0 , N):
temp[arr[i] - 1 ] = 1
for i in range ( 0 , N + 1 ):
if (temp[i] = = 0 ):
ans = i + 1
print (ans)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 5 ]
N = len (arr)
findMissing(arr, N)
|
C#
using System;
public class GFG {
public static void findMissing( int [] arr, int N)
{
int [] temp = new int [N + 1];
for ( int i = 0; i < N; i++) {
temp[arr[i] - 1] = 1;
}
int ans = 0;
for ( int i = 0; i <= N; i++) {
if (temp[i] == 0)
ans = i + 1;
}
Console.WriteLine(ans);
}
static public void Main()
{
int [] arr = { 1, 3, 7, 5, 6, 2 };
int n = arr.Length;
findMissing(arr, n);
}
}
|
Javascript
<script>
function findMissing(arr,N){
let i;
let temp = [];
for (i = 0; i <= N; i++) {
temp[i] = 0;
}
for (i = 0; i < N; i++) {
temp[arr[i] - 1] = 1;
}
let ans = 0;
for (i = 0; i <= N; i++) {
if (temp[i] == 0)
ans = i + 1;
}
console.log(ans);
}
let arr = [ 1, 3, 7, 5, 6, 2 ];
let n = arr.length;
findMissing(arr,n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach 2 (Using summation of first N natural numbers): The idea behind the approach is to use the summation of the first N numbers.
Find the sum of the numbers in the range [1, N] using the formula N * (N+1)/2. Now find the sum of all the elements in the array and subtract it from the sum of the first N natural numbers. This will give the value of the missing element.
Follow the steps mentioned below to implement the idea:
- Calculate the sum of the first N natural numbers as sumtotal= N*(N+1)/2.
- Traverse the array from start to end.
- Find the sum of all the array elements.
- Print the missing number as SumTotal – sum of array
Below is the implementation of the above approach:
C
#include <stdio.h>
int getMissingNo( int a[], int n)
{
int i, total;
total = (n + 1) * (n + 2) / 2;
for (i = 0; i < n; i++)
total -= a[i];
return total;
}
void main()
{
int arr[] = { 1, 2, 3, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int miss = getMissingNo(arr, N);
printf ( "%d" , miss);
}
|
C++14
#include <bits/stdc++.h>
using namespace std;
int getMissingNo( int a[], int n)
{
int N = n + 1;
int total = (N) * (N + 1) / 2;
for ( int i = 0; i < n; i++)
total -= a[i];
return total;
}
int main()
{
int arr[] = { 1, 2, 3, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int miss = getMissingNo(arr, N);
cout << miss;
return 0;
}
|
Java
import java.util.*;
import java.util.Arrays;
class GFG {
public static int getMissingNo( int [] nums, int n)
{
int sum = ((n + 1 ) * (n + 2 )) / 2 ;
for ( int i = 0 ; i < n; i++)
sum -= nums[i];
return sum;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 5 };
int N = arr.length;
System.out.println(getMissingNo(arr, N));
}
}
|
Python
def getMissingNo(arr, n):
total = (n + 1 ) * (n + 2 ) / 2
sum_of_A = sum (arr)
return total - sum_of_A
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 5 ]
N = len (arr)
miss = getMissingNo(arr, N)
print (miss)
|
C#
using System;
class GFG {
static int getMissingNo( int [] a, int n)
{
int total = (n + 1) * (n + 2) / 2;
for ( int i = 0; i < n; i++)
total -= a[i];
return total;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 5 };
int N = 4;
int miss = getMissingNo(arr, N);
Console.Write(miss);
}
}
|
PHP
<?php
function getMissingNo ( $a , $n )
{
$total = ( $n + 1) * ( $n + 2) / 2;
for ( $i = 0; $i < $n ; $i ++)
$total -= $a [ $i ];
return $total ;
}
$arr = array (1, 2, 3, 5);
$N = 4;
$miss = getMissingNo( $arr , $N );
echo ( $miss );
?>
|
Javascript
<script>
function getMissingNo(a, n) {
let total = Math.floor((n + 1) * (n + 2) / 2);
for (let i = 0; i < n; i++)
total -= a[i];
return total;
}
let arr = [ 1, 2, 3, 5 ];
let N = arr.length;
let miss = getMissingNo(arr, N);
document.write(miss);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Modification for Overflow: The approach remains the same but there can be an overflow if N is large.
In order to avoid integer overflow, pick one number from the range [1, N] and subtract a number from the given array (don’t subtract the same number twice). This way there won’t be any integer overflow.
Algorithm:
- Create a variable sum = 1 which will store the missing number and a counter variable c = 2.
- Traverse the array from start to end.
- Update the value of sum as sum = sum – array[i] + c and increment c by 1. This performs the task mentioned in the above idea]
- Print the missing number as a sum.
Below is the implementation of the above approach:
C
#include <stdio.h>
int getMissingNo( int a[], int n)
{
int i, total = 1;
for (i = 2; i < (n + 1); i++) {
total += i;
total -= a[i - 2];
}
return total;
}
void main()
{
int arr[] = { 1, 2, 3, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
printf ( "%d" , getMissingNo(arr, N));
}
|
C++14
#include <bits/stdc++.h>
using namespace std;
int getMissingNo( int a[], int n)
{
int i, total = 1;
for (i = 2; i < (n + 1); i++) {
total += i;
total -= a[i - 2];
}
return total;
}
int main()
{
int arr[] = { 1, 2, 3, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << getMissingNo(arr, N);
return 0;
}
|
Java
class GFG {
static int getMissingNo( int a[], int n)
{
int total = 1 ;
for ( int i = 2 ; i < (n + 1 ); i++) {
total += i;
total -= a[i - 2 ];
}
return total;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 5 };
int N = arr.length;
System.out.println(getMissingNo(arr, N));
}
}
|
Python3
def getMissingNo(a, n):
i, total = 0 , 1
for i in range ( 2 , n + 2 ):
total + = i
total - = a[i - 2 ]
return total
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 5 ]
N = len (arr)
print (getMissingNo(arr, N))
|
C#
using System;
class GFG {
static int getMissingNo( int [] a, int n)
{
int i, total = 1;
for (i = 2; i < (n + 1); i++) {
total += i;
total -= a[i - 2];
}
return total;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 5 };
int N = (arr.Length);
Console.Write(getMissingNo(arr, N));
}
}
|
Javascript
<script>
function getMissingNo(a, n)
{
let i, total=1;
for (i = 2; i< (n+1); i++)
{
total += i;
total -= a[i-2];
}
return total;
}
let arr = [1, 2, 3, 5];
let N = arr.length;
document.write(getMissingNo(arr, N));
</script>
|
Time Complexity: O(N). Only one traversal of the array is needed.
Auxiliary Space: O(1). No extra space is needed
Approach 3 (Using binary operations): This method uses the technique of XOR to solve the problem.
XOR has certain properties
- Assume a1 ⊕ a2 ⊕ a3 ⊕ . . . ⊕ an = a and a1 ⊕ a2 ⊕ a3 ⊕ . . . ⊕ an-1 = b
- Then a ⊕ b = an
Follow the steps mentioned below to implement the idea:
- Create two variables a = 0 and b = 0
- Run a loop from i = 1 to N:
- For every index, update a as a = a ^ i
- Now traverse the array from i = start to end.
- For every index, update b as b = b ^ arr[i].
- The missing number is a ^ b.
Below is the implementation of the above approach:
C
#include <stdio.h>
int getMissingNo( int a[], int n)
{
int i;
int x1 = a[0];
int x2 = 1;
for (i = 1; i < n; i++)
x1 = x1 ^ a[i];
for (i = 2; i <= n + 1; i++)
x2 = x2 ^ i;
return (x1 ^ x2);
}
void main()
{
int arr[] = { 1, 2, 3, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int miss = getMissingNo(arr, N);
printf ( "%d" , miss);
}
|
C++
#include <bits/stdc++.h>
using namespace std;
int getMissingNo( int a[], int n)
{
int x1 = a[0];
int x2 = 1;
for ( int i = 1; i < n; i++)
x1 = x1 ^ a[i];
for ( int i = 2; i <= n + 1; i++)
x2 = x2 ^ i;
return (x1 ^ x2);
}
int main()
{
int arr[] = { 1, 2, 3, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int miss = getMissingNo(arr, N);
cout << miss;
return 0;
}
|
Java
class Main {
static int getMissingNo( int a[], int n)
{
int x1 = a[ 0 ];
int x2 = 1 ;
for ( int i = 1 ; i < n; i++)
x1 = x1 ^ a[i];
for ( int i = 2 ; i <= n + 1 ; i++)
x2 = x2 ^ i;
return (x1 ^ x2);
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 3 , 5 };
int N = arr.length;
int miss = getMissingNo(arr, N);
System.out.println(miss);
}
}
|
Python3
def getMissingNo(a, n):
x1 = a[ 0 ]
x2 = 1
for i in range ( 1 , n):
x1 = x1 ^ a[i]
for i in range ( 2 , n + 2 ):
x2 = x2 ^ i
return x1 ^ x2
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 5 ]
N = len (arr)
miss = getMissingNo(arr, N)
print (miss)
|
C#
using System;
class GFG {
static int getMissingNo( int [] a, int n)
{
int x1 = a[0];
int x2 = 1;
for ( int i = 1; i < n; i++)
x1 = x1 ^ a[i];
for ( int i = 2; i <= n + 1; i++)
x2 = x2 ^ i;
return (x1 ^ x2);
}
public static void Main()
{
int [] arr = { 1, 2, 3, 5 };
int N = 4;
int miss = getMissingNo(arr, N);
Console.Write(miss);
}
}
|
PHP
<?php
function getMissingNo( $a , $n )
{
$x1 = $a [0];
$x2 = 1;
for ( $i = 1; $i < $n ; $i ++)
$x1 = $x1 ^ $a [ $i ];
for ( $i = 2; $i <= $n + 1; $i ++)
$x2 = $x2 ^ $i ;
return ( $x1 ^ $x2 );
}
$arr = array (1, 2, 3, 5);
$N = 4;
$miss = getMissingNo( $arr , $N );
echo ( $miss );
?>
|
Javascript
<script>
function getMissingNo(a, n)
{
var x1 = a[0];
var x2 = 1;
for ( var i = 1; i < n; i++) x1 = x1 ^ a[i];
for ( var i = 2; i <= n + 1; i++) x2 = x2 ^ i;
return x1 ^ x2;
}
var arr = [1, 2, 3, 5];
var N = arr.length;
var miss = getMissingNo(arr, N);
document.write(miss);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach 4 (Using Cyclic Sort): The idea behind it is as follows:
All the given array numbers are sorted and in the range of 1 to n-1. If the range is 1 to N then the index of every array element will be the same as (value – 1).
Follow the below steps to implement the idea:
- Use cyclic sort to sort the elements in linear time.
- Now traverse from i = 0 to the end of the array:
- If arr[i] is not the same as i+1 then the missing element is (i+1).
- If all elements are present then N is the missing element in the range [1, N].
Below is the implementation of the above approach.
C
#include <stdio.h>
int getMissingNo( int a[], int n)
{
int i = 0;
while (i < n) {
int correct = a[i] - 1;
if (a[i] < n && a[i] != a[correct]) {
int temp = a[i];
a[i] = a[correct];
a[correct] = temp;
}
else {
i++;
}
}
for ( int i = 0; i < n; i++) {
if (i != a[i] - 1) {
return i + 1;
}
}
return n;
}
int main()
{
int arr[] = { 1, 2, 3, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int miss = getMissingNo(arr, N);
printf ( "%d" , miss);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
int getMissingNo( int a[], int n)
{
int i = 0;
while (i < n) {
int correct = a[i] - 1;
if (a[i] < n && a[i] != a[correct]) {
swap(a[i], a[correct]);
}
else {
i++;
}
}
for ( int i = 0; i < n; i++) {
if (i != a[i] - 1) {
return i + 1;
}
}
return n;
}
int main()
{
int arr[] = { 1, 2, 3, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int miss = getMissingNo(arr, N);
cout << (miss);
return 0;
}
|
Java
import java.util.*;
public class MissingNumber {
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 5 };
int N = arr.length;
int ans = getMissingNo(arr, N);
System.out.println(ans);
}
static int getMissingNo( int [] arr, int n)
{
int i = 0 ;
while (i < n) {
int correctpos = arr[i] - 1 ;
if (arr[i] < n && arr[i] != arr[correctpos]) {
swap(arr, i, correctpos);
}
else {
i++;
}
}
for ( int index = 0 ; index < arr.length; index++) {
if (arr[index] != index + 1 ) {
return index + 1 ;
}
}
return arr.length;
}
static void swap( int [] arr, int i, int correctpos)
{
int temp = arr[i];
arr[i] = arr[correctpos];
arr[correctpos] = temp;
}
}
|
Python3
def getMissingNo(arr, n) :
i = 0 ;
while (i < n) :
correctpos = arr[i] - 1 ;
if (arr[i] < n and arr[i] ! = arr[correctpos]) :
arr[i],arr[correctpos] = arr[correctpos], arr[i]
else :
i + = 1 ;
for index in range (n) :
if (arr[index] ! = index + 1 ) :
return index + 1 ;
return n;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 5 ];
N = len (arr);
print (getMissingNo(arr, N));
|
C#
using System;
class GFG {
static int getMissingNo( int [] arr, int n)
{
int i = 0;
while (i < n) {
int correctpos = arr[i] - 1;
if (arr[i] < n && arr[i] != arr[correctpos]) {
swap(arr, i, correctpos);
}
else {
i++;
}
}
for ( int index = 0; index < n; index++) {
if (arr[index] != index + 1) {
return index + 1;
}
}
return n;
}
static void swap( int [] arr, int i, int correctpos)
{
int temp = arr[i];
arr[i] = arr[correctpos];
arr[correctpos] = temp;
}
public static void Main()
{
int [] arr = { 1, 2, 4, 5, 6 };
int N = arr.Length;
int ans = getMissingNo(arr, N);
Console.Write(ans);
}
}
|
Javascript
<script>
var arr = [ 1, 2, 3, 5 ];
var N = arr.length;
var ans = getMissingNo(arr, N);
console.log(ans);
function getMissingNo(arr, n)
{
var i = 0;
while (i < n) {
var correctpos = arr[i] - 1;
if (arr[i] < n && arr[i] != arr[correctpos]) {
swap(arr, i, correctpos);
}
else {
i++;
}
}
for ( var index = 0; index < arr.length; index++) {
if (arr[index] != index + 1) {
return index + 1;
}
}
return n;
}
function swap(arr, i, correctpos)
{
var temp = arr[i];
arr[i] = arr[correctpos];
arr[correctpos] = temp;
}
</script>
|
Time Complexity: O(N), requires (N-1) comparisons
Auxiliary Complexity: O(1)
Approach 5 (Use elements as Index and mark the visited places as negative): Use the below idea to get the approach
Traverse the array. While traversing, use the absolute value of every element as an index and make the value at this index as negative to mark it visited. To find missing, traverse the array again and look for a positive value.
Follow the steps to solve the problem:
- Traverse the given array
- If the absolute value of current element is greater than size of the array, then continue.
- else multiply the (absolute value of (current element) – 1)th index with -1.
- Initialize a variable ans = size + 1.
- Traverse the array and follow the steps:
- if the value is positive assign ans = index + 1
- Print ans as the missing value.
Below is the implementation of the above approach:
C
#include <stdio.h>
#include <stdlib.h>
void findMissing( int arr[], int size)
{
int i;
for (i = 0; i < size; i++) {
if ( abs (arr[i]) - 1 == size) {
continue ;
}
int ind = abs (arr[i]) - 1;
arr[ind] *= -1;
}
int ans = size + 1;
for (i = 0; i < size; i++) {
if (arr[i] > 0)
ans = i + 1;
}
printf ( "%d" , ans);
}
int main()
{
int arr[] = { 1, 3, 7, 5, 6, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
findMissing(arr, n);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
void findMissing( int arr[], int size)
{
int i;
for (i = 0; i < size; i++) {
if ( abs (arr[i]) - 1 == size){
continue ;
}
int ind = abs (arr[i]) - 1;
arr[ind] *= -1;
}
int ans = size + 1;
for (i = 0; i < size; i++) {
if (arr[i] > 0)
ans = i + 1;
}
cout << ans;
}
int main()
{
int arr[] = { 1, 3, 7, 5, 6, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
findMissing(arr, n);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void findMissing( int arr[], int size)
{
int i;
for (i = 0 ; i < size; i++) {
if (Math.abs(arr[i]) - 1 == size) {
continue ;
}
int ind = Math.abs(arr[i]) - 1 ;
arr[ind] *= - 1 ;
}
int ans = size + 1 ;
for (i = 0 ; i < size; i++) {
if (arr[i] > 0 )
ans = i + 1 ;
}
System.out.println(ans);
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 7 , 5 , 6 , 2 };
int n = arr.length;
findMissing(arr, n);
}
}
|
Python3
def findMissing(a, size):
for i in range ( 0 , n):
if ( abs (arr[i]) - 1 = = size):
continue
ind = abs (arr[i]) - 1
arr[ind] * = - 1
ans = size + 1
for i in range ( 0 , n):
if (arr[i] > 0 ):
ans = i + 1
print (ans)
if __name__ = = '__main__' :
arr = [ 1 , 3 , 7 , 5 , 6 , 2 ]
n = len (arr)
findMissing(arr, n)
|
C#
using System;
public class GFG {
public static void findMissing( int [] arr, int size)
{
for ( int i = 0; i < size; i++) {
if (Math.Abs(arr[i]) - 1 == size)
continue ;
int ind = Math.Abs(arr[i]) - 1;
arr[ind] *= -1;
}
int ans = size + 1;
for ( int i = 0; i < size; i++) {
if (arr[i] > 0)
ans = i + 1;
}
Console.WriteLine(ans);
}
static public void Main()
{
int [] arr = { 1, 3, 7, 5, 6, 2 };
int n = arr.Length;
findMissing(arr, n);
}
}
|
Javascript
<script>
function findMissing(arr,size){
let i;
for (i = 0; i < size; i++) {
if (Math.abs(arr[i]) - 1 == size) {
continue ;
}
let ind = Math.abs(arr[i]) - 1;
arr[ind] *= -1;
}
let ans = size + 1;
for (i = 0; i < size; i++) {
if (arr[i] > 0)
ans = i + 1;
}
console.log(ans);
}
let arr = [ 1, 3, 7, 5, 6, 2 ];
let n = arr.length;
findMissing(arr,n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...