Find the missing number in a sorted array of limited range

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).

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find missing Number in
// a sorted array of size n and distinct
// elements.
#include<bits/stdc++.h>
using namespace std;
  
// Function to find missing number
int getMissingNo(int a[], int n)
{
    for (int i=0; i<n; i++)        
        if (a[i] != (i+1))
            return (i+1);
   
    // If all numbers from 1 to n
    // are present
    return n+1;
}
  
// Driver code
int main()
{
    int a[] = {1, 2, 4, 5, 6};
    int n = sizeof(a) / sizeof(a[0]);
    cout << getMissingNo(a, n);
    return 0;
}
  
// This code is contributed by Sachin Bisht

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find missing Number in
// a sorted array of size n and distinct
// elements.
class Main
{
    // Function to find missing number
    static int getMissingNo(int a[])
    {
        int n = a.length;
        for (int i=0; i<n; i++)        
            if (a[i] != (i+1))
                return (i+1);
  
        // If all numbers from 1 to n
        // are present
        return n+1;
    }
        
    /* program to test above function */
    public static void main(String args[])
    {
        int a[] = {1, 2, 4, 5, 6};
        System.out.println(getMissingNo(a));
    }
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find missing Number in
# a sorted array of size n and distinct
# elements.
  
# function to find missing number
def getMissingNo(a):
    n = len(a)
      
    for i in range(n):
        if(a[i] != i + 1):
            return i + 1
              
    # If all numbers from 1 to n
    # are present
    return n+1
  
# Driver code
a = [1, 2, 4, 5, 6]
print(getMissingNo(a))
  
# This code is contributed by Sachin Bisht

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find missing Number 
// in a sorted array of size n and 
// distinct elements.
using System;
  
class GFG
{
      
// Function to find missing number
static int getMissingNo(int []a, int n)
{
    for (int i = 0; i < n; i++) 
        if (a[i] != (i + 1))
            return (i + 1);
  
    // If all numbers from
    // 1 to n are present
    return n + 1;
}
  
// Driver code
public static void Main()
{
    int []a = {1, 2, 4, 5, 6};
    int n = a.Length;
    Console.WriteLine(getMissingNo(a, n));
}
}
  
// This code is contributed by ihritik

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find missing Number 
// in a sorted array of size n and
// distinct elements.
  
// Function to find missing number
function getMissingNo($a, $n)
{
    for ($i = 0; $i < $n; $i++) 
        if ($a[$i] != ($i + 1))
            return ($i + 1);
  
    // If all numbers from 
    // 1 to n are present
    return $n + 1;
}
  
// Driver code
$a = array(1, 2, 4, 5, 6);
$n = sizeof($a);
echo getMissingNo($a, $n);
  
// This code is contributed 
// by ihritik
?>

chevron_right



Output :

3

This article is contributed by Balaguru. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : ihritik