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, 10, 20, 40}

Output:32

**Naive Approach:**

The simplest approach to solve the problem is to iterate through the array and calculate the sum of all the adjacent differences of the elements of the array.

**Time Complexity: **O(N)

**Auxiliary Space:** O(1)

**Efficient Approach:**

To optimize the above approach, the idea is to observe that the total count of numbers in the range of **[arr[0], arr[N – 1]]** is given by **arr[N-1] – arr[0] + 1**. Since the size of the array is **N**, the count of missing integers in the array is given by **arr[N-1] – arr[0] + 1 – N**.

Below is the implementation of the above approach:

## C++

`// C++ Program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that find the count of ` `// missing numbers in array a[] ` `void` `countMissingNum(` `int` `a[], ` `int` `N) ` `{ ` ` ` `// Calculate the count of missing ` ` ` `// numbers in the array ` ` ` `int` `count = a[N - 1] - a[0] + 1 - N; ` ` ` ` ` `cout << count << endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 5, 10, 20, 40 }; ` ` ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `countMissingNum(arr, N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `// Function that find the count of ` `// missing numbers in array a[] ` `public` `static` `void` `countMissingNum(` `int` `[] a, ` ` ` `int` `N) ` `{ ` ` ` ` ` `// Calculate the count of missing ` ` ` `// numbers in the array ` ` ` `int` `count = a[N - ` `1` `] - a[` `0` `] + ` `1` `- N; ` ` ` ` ` `System.out.println(count); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `5` `, ` `10` `, ` `20` `, ` `40` `}; ` ` ` ` ` `int` `N = arr.length; ` ` ` ` ` `countMissingNum(arr, N); ` `} ` `} ` ` ` `// This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function that find the count of ` `# missing numbers in array a[] ` `def` `countMissingNum(a, N): ` ` ` ` ` `# Calculate the count of missing ` ` ` `# numbers in the array ` ` ` `count ` `=` `a[N ` `-` `1` `] ` `-` `a[` `0` `] ` `+` `1` `-` `N ` ` ` ` ` `print` `(count) ` ` ` `# Driver Code ` `arr ` `=` `[ ` `5` `, ` `10` `, ` `20` `, ` `40` `] ` ` ` `N ` `=` `len` `(arr) ` ` ` `countMissingNum(arr, N) ` ` ` `# This code is contributed by sanjoy_62 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function that find the count of ` `// missing numbers in array a[] ` `public` `static` `void` `countMissingNum(` `int` `[] a, ` ` ` `int` `N) ` `{ ` ` ` ` ` `// Calculate the count of missing ` ` ` `// numbers in the array ` ` ` `int` `count = a[N - 1] - a[0] + 1 - N; ` ` ` ` ` `Console.Write(count); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `[]arr = { 5, 10, 20, 40 }; ` ` ` `int` `N = arr.Length; ` ` ` ` ` `countMissingNum(arr, N); ` `} ` `} ` ` ` `// This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

**Output:**

32

**Time Complexity:** O(1)

**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find missing element in a sorted array of consecutive numbers
- Find all missing numbers from a given sorted array
- Find the missing number in a sorted array of limited range
- k-th missing element in sorted array
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Find four missing numbers in an array containing elements from 1 to N
- Find first k natural numbers missing in given array
- Sort a nearly sorted (or K sorted) array
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Find Two Missing Numbers | Set 1 (An Interesting Linear Time Solution)
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Maximum number of partitions that can be sorted individually to make sorted
- Find the smallest positive number missing from an unsorted array | Set 1
- Find missing number in another array which is shuffled copy
- Find the missing integer in an array if mean is given
- Find the smallest positive number missing from an unsorted array | Set 2
- Smallest prime number missing in an array
- k-th missing element in an unsorted array
- Find the smallest positive number missing from an unsorted array : Hashing Implementation

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.