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 : 2Input : 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++
// 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 |
Java
// Java program to find missing Number in // a sorted array of size n and distinct // elements. import java.io.*; public 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)); } } |
Python3
# 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 |
C#
// 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 |
PHP
<?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 ?> |
Javascript
<script> // javascript 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 (let 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 let a = [1, 2, 4, 5, 6] let n = a.length; document.write(getMissingNo(a, n)); // This code is contributed by mohit kumar 29. </script> |
3
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++
// 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) { // Total sum required int total_sum = ((n + 1) * (n + 2)) / 2; // Current sum of given array int sum = 0; for ( int i = 0; i < n; i++) sum += a[i]; return total_sum - sum; } // 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 Pushpesh Raj |
Java
// Java program to find missing Number in a sorted array of // size n and distinct elements. import java.io.*; class GFG { // Function to find missing number static int getMissingNo( int [] a, int n) { // Total sum required int total_sum = ((n + 1 ) * (n + 2 )) / 2 ; // Current sum of given array 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)); } } // This code is contributed by lokeshmvs21. |
Python3
class GFG : # Function to find missing number @staticmethod def getMissingNo( a, n) : # Total sum required total_sum = int (((n + 1 ) * (n + 2 )) / 2 ) # Current sum of given array 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([]) # This code is contributed by aadityaburujwale. |
C#
// C# program to find missing Number in a sorted array of // size n and distinct elements. using System; public class GFG { // Function to find missing number static int getMissingNo( int [] a, int n) { // Total sum required int total_sum = ((n + 1) * (n + 2)) / 2; // Current sum of given array int sum = 0; for ( int i = 0; i < n; i++) { sum += a[i]; } return total_sum - sum; } static public void Main() { // Code int [] a = { 1, 2, 4, 5, 6 }; int n = a.Length; Console.Write(getMissingNo(a, n)); } } // This code is contributed by lokeshmvs21. |
Javascript
<script> // Function to find missing number function getMissingNo( a, n) { // Total sum required let total_sum = ((n + 1) * (n + 2)) / 2; // Current sum of given array 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> |
3
Time Complexity: O(N), Where N is the length of the given array.
Auxiliary Space: O(1)
This article is contributed by Balaguru. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Login to comment...