

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Finding one missing number in a scrambled sequence using JavaScript
- 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++
- Find the missing number in Arithmetic Progression in C++
- Find the missing number in Geometric Progression in C++
- Find missing numbers in a sorted list range in Python
- Program to find first positive missing integer in range in Python
- Find missing number in a sequence in C#
- Missing Number In Arithmetic Progression using C++
- How to find the missing number and the repeated number in a sorted array without using any inbuilt functions using 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++
- Using activity in SAP for number range intervals and number range objects
- Missing Number in Python