Open In App

Find the missing number in a sorted array of limited range

Last Updated : 22 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a sorted array of size n, consisting of integers from 1 to n+1 with one missing. The task is to find the missing element.

Examples: 

Input : arr[] = [1, 3, 4, 5, 6]
Output : 2

Input  : arr[] = [1, 2, 3, 4, 5, 7, 8, 9, 10]
Output  : 6

Input: arr[] = [1, 2, 3, 4]
Output: 5

Using Linear Search – O(n) time and O(1) space

The idea is to 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). 

C++
// C++ program to find the missing number 
// in a sorted array of limited range
#include <bits/stdc++.h>
using namespace std;

// Function to find the missing number 
// in a sorted array of limited range
int missingVal(vector<int> &arr) {
    int n = arr.size(); 
    
    // checking the difference between 
    // index and element
    for (int i = 0; i < n; i++) {
        
        // if difference between index and element 
        // is not 1
        if( arr[i] != i+1) {
            return i+1; 
        }
    }
    
    // If all elements in range [1, n] are
    // present, then n+1 is not present.
    return n + 1;
}


int main()  {
    vector<int> arr = {1, 3, 4, 5, 6};
    cout << missingVal(arr);
    return 0;
}
Java
// Java program to find the missing number 
// in a sorted array of limited range
import java.util.*;

class GfG {

    // Function to find the missing number 
    // in a sorted array of limited range
    static int missingVal(int[] arr) {
        int n = arr.length; 
        
        // checking the difference between 
        // index and element
        for (int i = 0; i < n; i++) {
            
            // if difference between index and element 
            // is not 1
            if( arr[i] != i+1) {
                return i+1; 
            }
        }
        
        // If all elements in range [1, n] are
        // present, then n+1 is not present.
        return n + 1;
    }

    public static void main(String[] args) {
        int[] arr = {1, 3, 4, 5, 6};
        System.out.println(missingVal(arr));
    }
}
Python
# Python program to find the missing number 
# in a sorted array of limited range

# Function to find the missing number 
# in a sorted array of limited range
def missingVal(arr):
    n = len(arr)
    
    # checking the difference between 
    # index and element
    for i in range(n):
        
        # if difference between index and element 
        # is not 1
        if arr[i] != i + 1:
            return i + 1
    
    # If all elements in range [1, n] are
    # present, then n+1 is not present.
    return n + 1

if __name__ == "__main__":
    arr = [1, 3, 4, 5, 6]
    print(missingVal(arr))
C#
// C# program to find the missing number 
// in a sorted array of limited range
using System;

class GfG {

    // Function to find the missing number 
    // in a sorted array of limited range
    static int missingVal(int[] arr) {
        int n = arr.Length;
        
        // checking the difference between 
        // index and element
        for (int i = 0; i < n; i++) {
            
            // if difference between index and element 
            // is not 1
            if (arr[i] != i + 1) {
                return i + 1;
            }
        }
        
        // If all elements in range [1, n] are
        // present, then n+1 is not present.
        return n + 1;
    }

    static void Main() {
        int[] arr = {1, 3, 4, 5, 6};
        Console.WriteLine(missingVal(arr));
    }
}
JavaScript
// JavaScript program to find the missing number 
// in a sorted array of limited range

// Function to find the missing number 
// in a sorted array of limited range
function missingVal(arr) {
    let n = arr.length;
    
    // checking the difference between 
    // index and element
    for (let i = 0; i < n; i++) {
        
        // if difference between index and element 
        // is not 1
        if (arr[i] != i + 1) {
            return i + 1;
        }
    }
    
    // If all elements in range [1, n] are
    // present, then n+1 is not present.
    return n + 1;
}

let arr = [1, 3, 4, 5, 6];
console.log(missingVal(arr));

Output
2

Using Formula for Sum of first n Natural Numbers – O(n) time and O(1) space

The idea is to precompute the sum of first n + 1 natural numbers. Then subtract the value of each element of array from this sum. The remaining value will be the missing value.

The formula used to find the sum of first n natural numbers is (n * (1 + n)) / 2.

C++
// C++ program to find the missing number 
// in a sorted array of limited range
#include <bits/stdc++.h>
using namespace std;

// Function to find the missing number 
// in a sorted array of limited range
int missingVal(vector<int> &arr) {
    int n = arr.size(); 
    
    int sum = ((n + 1) * (1 + n + 1)) / 2;
    
    // Subtract each value from sum.
    for (int i = 0; i < n; i++) {
        
        sum -= arr[i];
    }
    
    // The remaining value is the missing 
    // value.
    return sum;
}


int main()  {
    vector<int> arr = {1, 3, 4, 5, 6};
    cout << missingVal(arr);
    return 0;
}
Java
// Java program to find the missing number 
// in a sorted array of limited range
import java.util.*;

class GfG {

    // Function to find the missing number 
    // in a sorted array of limited range
    static int missingVal(int[] arr) {
        int n = arr.length; 
        
        int sum = ((n + 1) * (1 + n + 1)) / 2;
        
        // Subtract each value from sum.
        for (int i = 0; i < n; i++) {
            sum -= arr[i];
        }
        
        // The remaining value is the missing 
        // value.
        return sum;
    }

    public static void main(String[] args) {
        int[] arr = {1, 3, 4, 5, 6};
        System.out.println(missingVal(arr));
    }
}
Python
# Python program to find the missing number 
# in a sorted array of limited range

# Function to find the missing number 
# in a sorted array of limited range
def missingVal(arr):
    n = len(arr)
    
    sum = ((n + 1) * (1 + n + 1)) // 2
    
    # Subtract each value from sum.
    for i in range(n):
        sum -= arr[i]
    
    # The remaining value is the missing 
    # value.
    return sum

if __name__ == "__main__":
    arr = [1, 3, 4, 5, 6]
    print(missingVal(arr))
C#
// C# program to find the missing number 
// in a sorted array of limited range
using System;

class GfG {

    // Function to find the missing number 
    // in a sorted array of limited range
    static int missingVal(int[] arr) {
        int n = arr.Length;
        
        int sum = ((n + 1) * (1 + n + 1)) / 2;
        
        // Subtract each value from sum.
        for (int i = 0; i < n; i++) {
            sum -= arr[i];
        }
        
        // The remaining value is the missing 
        // value.
        return sum;
    }

    static void Main() {
        int[] arr = {1, 3, 4, 5, 6};
        Console.WriteLine(missingVal(arr));
    }
}
JavaScript
// JavaScript program to find the missing number 
// in a sorted array of limited range

// Function to find the missing number 
// in a sorted array of limited range
function missingVal(arr) {
    let n = arr.length;
    
    let sum = ((n + 1) * (1 + n + 1)) / 2;
    
    // Subtract each value from sum.
    for (let i = 0; i < n; i++) {
        sum -= arr[i];
    }
    
    // The remaining value is the missing 
    // value.
    return sum;
}

let arr = [1, 3, 4, 5, 6];
console.log(missingVal(arr));

Output
2

Using XOR Operation – O(n) time and O(1) space

The idea is to find the XOR value of first n+1 natural numbers. Then XOR this value with each value of the array. The resultant value will be the missing value.

This works because XOR of two equal values is always 0. The values present in the array will cancel out the values from 1 to n+1. The only value left will be the missing value.

Step by step approach:

  1. Find the XOR value of all natural numbers from 1 to n+1.
  2. XOR this value with each value present in the array.
  3. Return this value.
C++
// C++ program to find the missing number 
// in a sorted array of limited range
#include <bits/stdc++.h>
using namespace std;

// Function to find the missing number 
// in a sorted array of limited range
int missingVal(vector<int> &arr) {
    int n = arr.size(); 
    
    int xorVal = 0;
    
    // Find xor of all values from 1 to n+1.
    for (int i=1; i<=n+1; i++) {
        xorVal ^= i;
    }
    
    // xor each value of array
    for (int i = 0; i < n; i++) {
        xorVal ^= arr[i];
    }
    
    // The remaining xor value is the  
    // missing value.
    return xorVal;
}


int main()  {
    vector<int> arr = {1, 3, 4, 5, 6};
    cout << missingVal(arr);
    return 0;
}
Java
// Java program to find the missing number 
// in a sorted array of limited range
import java.util.*;

class GfG {

    // Function to find the missing number 
    // in a sorted array of limited range
    static int missingVal(int[] arr) {
        int n = arr.length; 
        
        int xorVal = 0;
        
        // Find xor of all values from 1 to n+1.
        for (int i = 1; i <= n + 1; i++) {
            xorVal ^= i;
        }
        
        // xor each value of array
        for (int i = 0; i < n; i++) {
            xorVal ^= arr[i];
        }
        
        // The remaining xor value is the  
        // missing value.
        return xorVal;
    }

    public static void main(String[] args) {
        int[] arr = {1, 3, 4, 5, 6};
        System.out.println(missingVal(arr));
    }
}
Python
# Python program to find the missing number 
# in a sorted array of limited range

# Function to find the missing number 
# in a sorted array of limited range
def missingVal(arr):
    n = len(arr)
    
    xorVal = 0
    
    # Find xor of all values from 1 to n+1.
    for i in range(1, n + 2):
        xorVal ^= i
    
    # xor each value of array
    for i in range(n):
        xorVal ^= arr[i]
    
    # The remaining xor value is the  
    # missing value.
    return xorVal

if __name__ == "__main__":
    arr = [1, 3, 4, 5, 6]
    print(missingVal(arr))
C#
// C# program to find the missing number 
// in a sorted array of limited range
using System;

class GfG {

    // Function to find the missing number 
    // in a sorted array of limited range
    static int missingVal(int[] arr) {
        int n = arr.Length;
        
        int xorVal = 0;
        
        // Find xor of all values from 1 to n+1.
        for (int i = 1; i <= n + 1; i++) {
            xorVal ^= i;
        }
        
        // xor each value of array
        for (int i = 0; i < n; i++) {
            xorVal ^= arr[i];
        }
        
        // The remaining xor value is the  
        // missing value.
        return xorVal;
    }

    static void Main() {
        int[] arr = {1, 3, 4, 5, 6};
        Console.WriteLine(missingVal(arr));
    }
}
JavaScript
// JavaScript program to find the missing number 
// in a sorted array of limited range

// Function to find the missing number 
// in a sorted array of limited range
function missingVal(arr) {
    let n = arr.length;
    
    let xorVal = 0;
    
    // Find xor of all values from 1 to n+1.
    for (let i = 1; i <= n + 1; i++) {
        xorVal ^= i;
    }
    
    // xor each value of array
    for (let i = 0; i < n; i++) {
        xorVal ^= arr[i];
    }
    
    // The remaining xor value is the  
    // missing value.
    return xorVal;
}

let arr = [1, 3, 4, 5, 6];
console.log(missingVal(arr));

Output
2

Using Binary Search – O(Log n) time and O(1) space

In each iteration, we calculate the middle index and check if the value at that index equals its 1-based position (mid+1). If arr[mid] equals mid+1, it means all elements up to the middle are in their correct positions, so the missing element must be on the right side. We update left = mid+1. If arr[mid] equals mid+2, it indicates that mid+1 is the missing value or the missing value is on the left side, so we update right = mid-1.

Refer to Missing in a Sorted Array of Natural Numbers for detailed approach and code.




Next Article

Similar Reads