Find the missing number in a sorted array of limited range
Last Updated :
22 Apr, 2025
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));
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));
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:
- Find the XOR value of all natural numbers from 1 to n+1.
- XOR this value with each value present in the array.
- 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));
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.
Similar Reads
Find the only missing number in a sorted array You are given a sorted array of N integers from 1 to N with one number missing find the missing number Examples: Input :ar[] = {1, 3, 4, 5}Output : 2Input : ar[] = {1, 2, 3, 4, 5, 7, 8}Output : 6A simple solution is to linearly traverse the given array. Find the point where current element is not on
9 min read
Count of Missing Numbers in a sorted array Given a sorted array arr[], the task is to calculate the number of missing numbers between the first and last element of the sorted array. Examples: Input: arr[] = { 1, 4, 5, 8 } Output: 4 Explanation: The missing integers in the array are {2, 3, 6, 7}. Therefore, the count is 4. Input: arr[] = {5,
9 min read
Kth Missing Positive Number in a Sorted Array Given a sorted array of distinct positive integers arr[] and integer k, the task is to find the kth positive number that is missing from arr[].Examples : Input: arr[] = [2, 3, 4, 7, 11], k = 5Output: 9Explanation: Missing are 1, 5, 6, 8, 9, 10, ... and 5th missing number is 9.Input: arr[] = [1, 2, 3
10 min read
Find the one missing number in range Given an array of size n. It is also given that range of numbers is from smallestNumber to smallestNumber + n where smallestNumber is the smallest number in array. The array contains number in this range but one number is missing so the task is to find this missing number. Examples: Input: arr[] = {
9 min read
Find the Nth number in the merged and sorted lists of given ranges Given two integer array, L and R and an integer N. Each range from the given array denote that every number in the range [L[i], R[i]] is present. The task is to calculate the N-th(0-based indexing) element when numbers from given ranges are ordered in their sorted order. Examples: Input: L = {1, 5},
7 min read
Find all missing numbers from a given sorted array Given a sorted array arr[] of N integers, The task is to find the multiple missing elements in the array between the ranges [arr[0], arr[N-1]]. Examples: Input: arr[] = {6, 7, 10, 11, 13}Output: 8 9 12 Explanation: The elements of the array are present in the range of the maximum and minimum array e
13 min read
Find the missing number in range [1, N*M+1] represented as Matrix of size N*M Given an N x M matrix mat[][] where all elements are natural numbers starting from 1 and are continuous except 1 element, find that element. Examples: Input: mat[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 11, 12, 13}}N = 3, M = 4Output: 10Explanation: Missing number is 10 at row no 3. Input: mat[][] = {
15+ min read
Missing in a Sorted Array of Natural Numbers Given a sorted array arr[] of n-1 integers, these integers are in the range of 1 to n. There are no duplicates in the array. One of the integers is missing in the array. Write an efficient code to find the missing integer. Examples: Input : arr[] = [1, 2, 3, 4, 6, 7, 8]Output : 5Explanation: The mis
12 min read
Find missing element in a sorted array of consecutive numbers Given an array arr[] of n distinct integers. Elements are placed sequentially in ascending order with one element missing. The task is to find the missing element.Examples: Input: arr[] = {1, 2, 4, 5, 6, 7, 8, 9} Output: 3Input: arr[] = {-4, -3, -1, 0, 1, 2} Output: -2Input: arr[] = {1, 2, 3, 4} Out
7 min read
Find the Target number in an Array Finding a number within an array is an operation, in the field of computer science and data analysis. In this article, we will discuss the steps involved and analyze their time and space complexities. Examples: Input: Array: {10, 20, 30, 40, 50} , Target: 30Output: "Target found at index 2" Input: A
13 min read