Count of missing elements from 1 to maximum in index range [L, R]
Given an array arr[] of length N, and an array queries[] of size Q, the task is to find the number of missing elements from 1 to maximum in the range of indices [L, R] where L is queries[i][0] and R is queries[i][1].
Examples:
Input: arr[] = {8, 6, 7, 7, 7}, queries = { {0, 3}, {1, 2}, {0, 2}, {1, 3}, {2, 3} }
Output: 5 5 5 5 6
Explanation:
Maximum element for range [0, 3] is 8 so number of missing elements are 1, 2, 3, 4, 5.
Maximum element for range [1, 2] is 7 so number of missing elements are 1, 2, 3, 4, 5.
Maximum element for range [0, 2] is 8 so number of missing elements are 1, 2, 3, 4, 5.
Maximum element for range [1, 3] is 7 so number of missing elements are 1, 2, 3, 4, 5.
Maximum element for range [2, 3] is 7 so number of missing elements are 1, 2, 3, 4, 5, 6.Input: arr[] = {2, 6, 4, 9}, queries = { {0, 3}, {1, 2}, {0, 2}, {1, 3}, {2, 3} }
Output: 5 4 3 6 7
Explanation:
Maximum element for range [0, 3] is 9 so number of missing elements are 1, 3, 5, 7, 8.
Maximum element for range [1, 2] is 6 so number of missing elements are 1, 2, 3, 5.
Maximum element for range [0, 2] is 6 so number of missing elements are 1, 3, 5.
Maximum element for range [1, 3] is 9 so number of missing elements are 1, 2, 3, 5, 7, 8.
Maximum element for range [2, 3] is 9 so number of missing elements are 1, 2, 3, 5, 6, 7, 8.
Approach: One simple way is to process every query linearly, store all elements in a visited map and find the maximum element. Then return the number of missing elements from 1 to the maximum on that range.
Follow the below steps to solve this problem:
- Take one unordered map visited.
- Traverse queries from i = 0 to Q-1.
- Initialize one variable to store the maximum element of the given range.
- Run again one for loop from queries[i][0] to queries[i][1].
- Store the maximum element in this range.
- Make the visited array 1 for each element visited in that range.
- The number of missing elements is the same as the value of (maximum element – number of unique elements visited).
Below is the implementation of the above approach.
C++14
// C++ code to implement above approach. #include <bits/stdc++.h>using namespace std; // Function to find the number// of mising element for each queryvector<int> SolveQMax(int* arr, int& n, int queries[][2], int& q){ // Initializing an unordered map unordered_map<int, bool> visited; int maxElement; vector<int> res; // Loop to find number of missing elements for (int i = 0; i < q; i++) { maxElement = INT_MIN; visited.clear(); for (int j = queries[i][0]; j <= queries[i][1]; j++) { // Finding the maximum value maxElement = max(maxElement, arr[j]); visited[arr[j]] = 1; } res.push_back(maxElement - visited.size()); } // Return the answer return res} // Driver codeint main(){ int N = 5; int arr[] = { 8, 6, 7, 7, 7 }; int Q = 5; int queries[][2] = { { 0, 3 }, { 1, 2 }, { 0, 2 }, { 1, 3 }, { 2, 3 } }; // Function call SolveQMax(arr, N, queries, Q); return 0;} |
5 5 5 5 6
Time Complexity: O(Q * N)
Auxiliary Space: O(N)


