Count of missing elements from 1 to maximum in index range [L, R]
Last Updated :
24 May, 2022
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
#include <bits/stdc++.h>
using namespace std;
vector< int > SolveQMax( int * arr, int & n,
int queries[][2], int & q)
{
unordered_map< int , bool > visited;
int maxElement;
vector< int > res;
for ( int i = 0; i < q; i++) {
maxElement = INT_MIN;
visited.clear();
for ( int j = queries[i][0];
j <= queries[i][1]; j++) {
maxElement = max(maxElement,
arr[j]);
visited[arr[j]] = 1;
}
res.push_back(maxElement
- visited.size());
}
return res
}
int 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 } };
SolveQMax(arr, N, queries, Q);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static ArrayList<Integer>
SolveQMax( int arr[], int n, int queries[][], int q)
{
HashMap<Integer, Boolean> visited = new HashMap<>();
int maxElement;
ArrayList<Integer> res = new ArrayList<>();
for ( int i = 0 ; i < q; i++) {
maxElement = Integer.MIN_VALUE;
visited.clear();
for ( int j = queries[i][ 0 ]; j <= queries[i][ 1 ];
j++) {
maxElement = Math.max(maxElement, arr[j]);
visited.put(arr[j], true );
}
res.add(maxElement - visited.size());
}
return res;
}
public static void main(String[] args)
{
int N = 5 ;
int arr[] = { 8 , 6 , 7 , 7 , 7 };
int Q = 5 ;
int queries[][] = {
{ 0 , 3 }, { 1 , 2 }, { 0 , 2 }, { 1 , 3 }, { 2 , 3 }
};
System.out.print(SolveQMax(arr, N, queries, Q));
}
}
|
Python3
INT_MIN = - 2147483647 - 1
def SolveQMax(arr, n, queries, q):
visited = {}
maxElement = INT_MIN
res = []
for i in range (q):
maxElement = INT_MIN
visited = {}
for j in range (queries[i][ 0 ],queries[i][ 1 ] + 1 ):
maxElement = max (maxElement,arr[j])
visited[arr[j]] = 1
res.append(maxElement - len (visited))
return res
N = 5
arr = [ 8 , 6 , 7 , 7 , 7 ]
Q = 5
queries = [[ 0 , 3 ], [ 1 , 2 ],[ 0 , 2 ], [ 1 , 3 ],[ 2 , 3 ]]
print (SolveQMax(arr, N, queries, Q))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static List< int > SolveQMax( int [] arr, int n,
int [,] queries, int q)
{
Dictionary< int , Boolean> visited = new Dictionary< int , bool >();
int maxElement;
List< int > res = new List< int >();
for ( int i = 0; i < q; i++)
{
maxElement = int .MinValue;
visited.Clear();
for ( int j = queries[i, 0]; j <= queries[i, 1];
j++)
{
maxElement = Math.Max(maxElement, arr[j]);
visited[arr[j]] = true ;
}
res.Add(maxElement - visited.Count);
}
return res;
}
public static void Main()
{
int N = 5;
int [] arr = { 8, 6, 7, 7, 7 };
int Q = 5;
int [,] queries = {
{ 0, 3 }, { 1, 2 }, { 0, 2 }, { 1, 3 }, { 2, 3 }
};
List< int > res = SolveQMax(arr, N, queries, Q);
for ( int i = 0; i < res.Count; i++)
{
Console.Write(res[i] + " " );
}
}
}
|
Javascript
<script>
const INT_MIN = -2147483647 - 1;
const SolveQMax = (arr, n, queries, q) => {
let visited = {};
let maxElement = INT_MIN;
let res = [];
for (let i = 0; i < q; i++) {
maxElement = INT_MIN;
visited = {};
for (let j = queries[i][0];
j <= queries[i][1]; j++) {
maxElement = Math.max(maxElement,
arr[j]);
visited[arr[j]] = 1;
}
res.push(maxElement
- Object.keys(visited).length);
}
return res
}
let N = 5;
let arr = [8, 6, 7, 7, 7];
let Q = 5;
let queries = [[0, 3], [1, 2],
[0, 2], [1, 3],
[2, 3]];
document.write(SolveQMax(arr, N, queries, Q));
</script>
|
Time Complexity: O(Q * N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...