
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the only missing number in a sorted array using C++
In this problem, we are given an arr[] of size N containing values from 1 to N with one value missing in the array. Our task is to find the only missing number in a sorted array.
Let’s take an example to understand the problem,
Input
arr[] = {1, 2, 3, 5, 6, 7}
Output
4
Solution Approach
A simple solution to the problem is by traversing the sorted array linearly. And then check for the missing value using the fact that arr[i] = (i + 1).
Example 1
Program to illustrate the working of our solution
#include <iostream> using namespace std; int findMissingValArray(int arr[], int N){ for(int i = 0; i < N; i++){ if(arr[i] != (i+1)) return (i+1); } return -1; } int main(){ int arr[] = {1, 2, 3, 4, 6}; int N = sizeof(arr)/sizeof(arr[0]); cout<<"The missing value from the array is "<<findMissingValArray(arr, N); return 0; }
Output
The missing value from the array is 5
Another approach to solve the problem is using binary search and checking the value at mid. If the value at mid is mid+1 and value at (mid - 1) is (mid) then we will traverse the left subarray and vise versa.
Example 2
Program to illustrate the working of our solution
#include <iostream> using namespace std; int findMissingValArray(int arr[], int N){ int s = 0, e = N - 1; while (s <= e) { int mid = (s + e) / 2; if (arr[mid] != mid + 1 && arr[mid - 1] == mid) return (mid + 1); if (arr[mid] != (mid + 1)) e = (mid - 1); else s = (mid + 1); } return -1; } int main(){ int arr[] = {1, 2, 3, 4, 6}; int N = sizeof(arr)/sizeof(arr[0]); cout<<"The missing value from the array is "<<findMissingValArray(arr, N); return 0; }
Output
The missing value from the array is 5
- Related Articles
- How to find the missing number and the repeated number in a sorted array without using any inbuilt functions using C#?
- Find the only repeating element in a sorted array of size n using C++
- Find missing element in a sorted array of consecutive numbers in Python
- Find missing element in a sorted array of consecutive numbers in C++
- Missing Element in Sorted Array in C++
- What are the different ways to find missing numbers in a sorted array without any inbuilt functions using C#?
- Find the number of elements greater than k in a sorted array using C++
- k-th missing element in sorted array in C++
- Find missing numbers in a sorted list range in Python
- Find the one missing number in range using C++
- How to find the number of times array is rotated in the sorted array by recursion using C#?
- Find the Smallest Positive Number Missing From an Unsorted Array
- How to find the missing number in a given Array from number 1 to n in Java?
- Check if the array can be sorted using swaps between given indices only in Python
- Find the repeating and the missing number using two equations in C++

Advertisements