Skip to content
Home » C++ Programs » C++ Program to Find the Smallest Element Missing in a Sorted Array

C++ Program to Find the Smallest Element Missing in a Sorted Array

C++ Program to Find the Smallest Element Missing in a Sorted Array. Using this program to the smallest missing element in the array. An array is a collection of elements of the same kind that are stored in contiguous memory locations and may be accessed separately using a unique identifier’s index. Without having to define five individual variables, an array of five int values can be declared.

Array in C++

The array is a data structure in C++ that stores a fixed-size sequential collection of objects of the same type in a fixed-size sequential order. Although an array is used to hold data, it is often more beneficial to conceive of it as a collection of variables of the same type. All arrays are made up of memory regions that are connected in some way. Using this program we can execute smallest missing element in c++ array.

Return Statement in C++

A return statement marks the end of a function’s execution and hands control back to the calling function. The calling function continues execution at the place where the call was made. A return statement can give the caller function a value.

For Loop in C++ Program

A for loop is a repetition control structure that lets you write a loop that needs to run a certain number of times quickly. A for-loop, or simply for loop, is a control flow statement in computer science that specifies iteration and allows code to be executed repeatedly. While-loops increment and test a loop variable, for-loops can be thought of as short hands for while-loops. 

C++ Program to Find the Smallest Element Missing in a Sorted Array

#include<iostream>
#include<algorithm>
using namespace std;
int small(int arr[], int start, int end)
{
if (start > end)
return end + 1;
if (start != arr[start])
return start;
int mid = (start + end) / 2;
if (arr[mid] == mid)
return small(arr, mid + 1, end);
return small(arr, start, mid);
}
int main()
{
int arr[100], n, i;
cout << "Enter number of elements";
cin >> n;
cout << "\nEnter elements";
for (i = 0; i < n; i++)
cin >> arr[i];
cout << "Original array";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
int answer;
answer = small(arr, 0, n - 1);
cout << "\nSmallest missing element is " << answer;
return 0;
}

Output:

Enter number of elements3

Enter elements45
67
89
Original array45 67 89
Smallest missing element is 0