
- 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 one missing number in range using C++
In this problem, we are given an arr[] of size n. Our task is to find the one missing number in range.
The array consists of all values ranging from smallest value to (smallest + n). One element of the range is missing from the array. And we need to find this missing value.
Let’s take an example to understand the problem,
Input
arr[] = {4, 8, 5, 7}
Output
6
Solution Approach
A simple solution to the problem is by searching the missing element by sorting the array and then finding the first element of range starting from minimum value which is not present in the array but present in the range.
This solution is a naive approach and will solve the problem is O(n log n) time complexity.
Another approach to solving the problem in less time is using XOR of the values of the array and the range. We will find the XOR of all values in the range and also find the XOR of all values of the array. The XOR of both these values will be our missing value.
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h> using namespace std; int findMissingNumArr(int arr[], int n){ int arrMin = *min_element(arr, arr+n); int numXor = 0; int rangeXor = arrMin; for (int i = 0; i < n; i++) { numXor ^= arr[i]; arrMin++; rangeXor ^= arrMin; } return numXor ^ rangeXor; } int main(){ int arr[] = { 5, 7, 4, 8, 9}; int n = sizeof(arr)/sizeof(arr[0]); cout<<"The missing value in the array is "<<findMissingNumArr(arr, n); return 0; }
Output
The missing value in the array is 6
- Related Articles
- Find missing elements of a range in C++
- Find the only missing number in a sorted array using C++
- Find the repeating and the missing number using two equations in C++
- Missing Number In Arithmetic Progression using C++
- Find the missing number in Arithmetic Progression in C++
- Find the missing number in Geometric Progression in C++
- Finding one missing number in a scrambled sequence using JavaScript
- Find missing number in a sequence in C#
- Find the Number of Prefix Sum Prime in Given Range Query using C++
- Find the Number Of Subarrays Having Sum in a Given Range using C++
- How to find the missing number and the repeated number in a sorted array without using any inbuilt functions using C#?
- Find missing numbers in a sorted list range in Python
- Program to find first positive missing integer in range in Python
- Find the Numbers that are not divisible by any number in the range [2, 10] using C++
- Find n-variables from n sum equations with one missing in C++
