
- 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 missing elements of a range in C++
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
#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
#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
#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
- Related Articles
- Print missing elements that lie in range 0 – 99
- Find missing numbers in a sorted list range in Python
- Find the one missing number in range using C++
- Find missing elements in List in Python
- Program to find first positive missing integer in range in Python
- Program to find the kth missing number from a list of elements in Python
- Find elements within range in numpy in Python
- Write a Golang program to find duplicate elements in a given range
- PHP program to find missing elements from an array
- Find a range that covers all the elements of given N ranges in C++
- Copy the elements of collection over a range of elements in ArrayList in C#
- Get the range of elements in a C# list
- Remove a range of elements from a LinkedList in Java
- Find smallest range containing elements from k lists in C++
- Remove a range of elements from the ArrayList in C#
