Find the missing number in a sorted array of limited range
Last Updated :
29 Aug, 2023
Given a sorted array of size n and given that there are numbers from 1 to n+1 with one missing, the missing number is to be found. It may be assumed that array has distinct elements.
Examples:
Input : 1 3 4 5 6
Output : 2
Input : 1 2 3 4 5 7 8 9 10
Output : 6
We traverse all elements. For every element a[i], we check if it is equal to i+1 or not. If not, we return (i+1).
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int getMissingNo( int a[], int n)
{
for ( int i=0; i<n; i++)
if (a[i] != (i+1))
return (i+1);
return n+1;
}
int main()
{
int a[] = {1, 2, 4, 5, 6};
int n = sizeof (a) / sizeof (a[0]);
cout << getMissingNo(a, n);
return 0;
}
|
Java
import java.io.*;
public class Main
{
static int getMissingNo( int a[])
{
int n = a.length;
for ( int i= 0 ; i<n; i++)
if (a[i] != (i+ 1 ))
return (i+ 1 );
return n+ 1 ;
}
public static void main(String args[])
{
int a[] = { 1 , 2 , 4 , 5 , 6 };
System.out.println(getMissingNo(a));
}
}
|
Python3
def getMissingNo(a):
n = len (a)
for i in range (n):
if (a[i] ! = i + 1 ):
return i + 1
return n + 1
a = [ 1 , 2 , 4 , 5 , 6 ]
print (getMissingNo(a))
|
C#
using System;
class GFG
{
static int getMissingNo( int []a, int n)
{
for ( int i = 0; i < n; i++)
if (a[i] != (i + 1))
return (i + 1);
return n + 1;
}
public static void Main()
{
int []a = {1, 2, 4, 5, 6};
int n = a.Length;
Console.WriteLine(getMissingNo(a, n));
}
}
|
Javascript
<script>
function getMissingNo(a,n)
{
for (let i = 0; i < n; i++)
if (a[i] != (i + 1))
return (i + 1);
return n + 1;
}
let a = [1, 2, 4, 5, 6]
let n = a.length;
document.write(getMissingNo(a, n));
</script>
|
PHP
<?php
function getMissingNo( $a , $n )
{
for ( $i = 0; $i < $n ; $i ++)
if ( $a [ $i ] != ( $i + 1))
return ( $i + 1);
return $n + 1;
}
$a = array (1, 2, 4, 5, 6);
$n = sizeof( $a );
echo getMissingNo( $a , $n );
?>
|
Time Complexity: O(N), Where N is the length of the given array.
Auxiliary Space: O(1)
Another Approach: (Use mathematical approach to solve this problem)
It is given, elements in the array are in the range of [1, n + 1], so first calculate the required total sum by adding all the numbers from 1 to n + 1, this can be calculated by using the formula of “sum of first N natural numbers” by sum = N*(N+1) / 2, where N is the first N natural numbers. After that subtract the required total sum by current sum of the given array. This will result in the missing number that are not present in the given array.
Implementation:
C++
#include <iostream>
#include <vector>
using namespace std;
int findMissingNumber( const vector< int >& nums)
{
int xor_sum = 0;
int n = nums.size();
for ( int i = 0; i < n; i++) {
xor_sum ^= nums[i]
^ (i + 1);
}
xor_sum ^= (n + 1);
return xor_sum;
}
int main()
{
vector< int > arr = { 1, 3, 4, 5, 6 };
int missingNum = findMissingNumber(arr);
cout << missingNum << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int getMissingNo( int [] a, int n)
{
int total_sum = ((n + 1 ) * (n + 2 )) / 2 ;
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
sum += a[i];
}
return total_sum - sum;
}
public static void main(String[] args)
{
int [] a = { 1 , 2 , 4 , 5 , 6 };
int n = a.length;
System.out.print(getMissingNo(a, n));
}
}
|
Python3
class GFG :
@staticmethod
def getMissingNo( a, n) :
total_sum = int (((n + 1 ) * (n + 2 )) / 2 )
sum = 0
i = 0
while (i < n) :
sum + = a[i]
i + = 1
return total_sum - sum
@staticmethod
def main( args) :
a = [ 1 , 2 , 4 , 5 , 6 ]
n = len (a)
print (GFG.getMissingNo(a, n), end = "")
if __name__ = = "__main__" :
GFG.main([])
|
C#
using System;
public class GFG {
static int getMissingNo( int [] a, int n)
{
int total_sum = ((n + 1) * (n + 2)) / 2;
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += a[i];
}
return total_sum - sum;
}
static public void Main()
{
int [] a = { 1, 2, 4, 5, 6 };
int n = a.Length;
Console.Write(getMissingNo(a, n));
}
}
|
Javascript
<script>
function getMissingNo( a, n)
{
let total_sum = ((n + 1) * (n + 2)) / 2;
let sum = 0;
for (let i = 0; i < n; i++)
sum += a[i];
return total_sum - sum;
}
let a = [1, 2, 4, 5, 6 ];
let n = 5;
document.write( getMissingNo(a, n));
</script>
|
Time Complexity: O(N), Where N is the length of the given array.
Auxiliary Space: O(1)
.
Approach: The task can be solved with the help of XOR operation. Follow the steps below to solve the problem:
- Initialize a variable called xor_sum to 0.
- Iterate through the array and perform the XOR operation between each element and its index.
- Store the result in xor_sum.
- Perform the XOR operation between xor_sum and the XOR of all numbers from 1 to n+1.
- Return the resulting xor_sum, which will be the missing number.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
using namespace std;
int findMissingNumber( const vector< int >& nums)
{
int xor_sum = 0;
int n = nums.size();
for ( int i = 0; i < n; i++) {
xor_sum ^= nums[i]
^ (i + 1);
}
xor_sum ^= (n + 1);
return xor_sum;
}
int main()
{
vector< int > arr = { 1, 3, 4, 5, 6 };
int missingNum = findMissingNumber(arr);
cout << missingNum << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
public static int findMissingNumber( int [] nums) {
int xorSum = 0 ;
int n = nums.length;
for ( int i = 0 ; i < n; i++) {
xorSum ^= nums[i]
^ (i + 1 );
}
xorSum ^= (n + 1 );
return xorSum;
}
public static void main(String[] args) {
int [] arr = { 1 , 3 , 4 , 5 , 6 };
int missingNum = findMissingNumber(arr);
System.out.println(missingNum);
}
}
|
Python3
def findMissingNumber(nums):
xorSum = 0
n = len (nums)
for i in range (n):
xorSum ^ = nums[i] ^ (i + 1 )
xorSum ^ = n + 1
return xorSum
arr = [ 1 , 3 , 4 , 5 , 6 ]
missingNum = findMissingNumber(arr)
print (missingNum)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int FindMissingNumber(List< int > nums)
{
int xorSum = 0;
int n = nums.Count;
for ( int i = 0; i < n; i++)
{
xorSum ^= nums[i]
^ (i + 1);
}
xorSum ^= (n + 1);
return xorSum;
}
static void Main( string [] args)
{
List< int > arr = new List< int > { 1, 3, 4, 5, 6 };
int missingNum = FindMissingNumber(arr);
Console.WriteLine(missingNum);
Console.ReadLine();
}
}
|
Javascript
function findMissingNumber(nums) {
let xorSum = 0;
let n = nums.length;
for (let i = 0; i < n; i++) {
xorSum ^= nums[i] ^ (i + 1);
}
xorSum ^= n + 1;
return xorSum;
}
let arr = [1, 3, 4, 5, 6];
let missingNum = findMissingNumber(arr);
console.log(missingNum);
|
Output:
2
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...