Find missing elements of a range in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[] of size n and the start and end element denoting the range. Our task is to Find missing elements of a range.

Problem Description − we will be finding the elements of the range that are not present in the range.

Let’s take an example to understand the problem,

Input

arr[] = {4, 6, 3, 7}, start = 3, end = 8

Output

5, 8

Explanation

The range is [3, 4, 5, 6, 7, 8]

The array is {4, 6, 3, 7}

Elements of range that are not present in array is 5, 8

Solution Approach

You can solve this problem in multiple ways. They are,

#Approach 1

One simple solution approach is by directly checking all elements of the range in the array. For this, we will sort the array and then find the first element of the range in the array and then find and print the elements that are missing.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void findMissingElements(int arr[], int n, int low, int high){
   sort(arr, arr + n);
   int* pointerVal = lower_bound(arr, arr + n, low);
   int index = pointerVal - arr;
   int i = index, x = low;
   while (i < n && x <= high) {
      if (arr[i] != x)
         cout << x << " ";
      else
         i++;
         x++;
   }
   while (x <= high)
      cout<<x++<<" ";
}
int main(){
   int arr[] = { 4, 6, 3, 7 };
   int n = sizeof(arr) / sizeof(arr[0]);
   int low = 3, high = 9;
   cout<<"The missing elements are ";
   findMissingElements(arr, n, low, high);
   return 0;
}

Output

The missing elements are 5 8 9

#Approach 2

Another approach to solve the problem is using an array. We will create a boolean array of size (end - start). For each element of this array, we will find if (i+start) is present in the array. If it is present, mark arr[i] = true else mark arr[i] = false. At the end, we will traverse the booleanArray and print all elements marked as false.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void findMissingElements(int arr[], int n, int start, int end){
   bool boolArray[end - start + 1] = { false };
   for (int i = 0; i < n; i++) {
      if (start <= arr[i] && arr[i] <= end)
      boolArray[arr[i] - start] = true;
   }
   for (int i = 0; i <= end - start; i++) {
      if (boolArray[i] == false)
         cout<<(start + i)<<"\t";
   }
}
int main(){
   int arr[] = { 4, 6, 3, 7 };
   int n = sizeof(arr) / sizeof(arr[0]);
   int low = 3, high = 9;
   cout<<"The missing elements are ";
   findMissingElements(arr, n, low, high);
   return 0;
}

Output

The missing elements are 5 8 9

#Approach 3

Another approach to solve the problem is using a hash table. Insert all the elements of the array into a hash table and after insertion traverse the range and print all elements that are not present in range.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void findMissingElements(int arr[], int n, int start, int end){
   unordered_set<int> arrEle;
   for (int i = 0; i < n; i++)
      arrEle.insert(arr[i]);
   for (int i = start; i <= end; i++)
      if (arrEle.find(i) == arrEle.end())
         cout<<i<<"\t";
}
int main(){
   int arr[] = { 4, 6, 3, 7 };
   int n = sizeof(arr) / sizeof(arr[0]);
   int low = 3, high = 9;
   cout<<"The missing elements are ";
   findMissingElements(arr, n, low, high);
   return 0;
}

Output

The missing elements are 5 8 9
raja
Published on 12-Mar-2021 07:31:05
Advertisements