Open In App

Count of missing elements from 1 to maximum in index range [L, R]

Last Updated : 24 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 missing element for each query
vector<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 code
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 } };
 
    // Function call
    SolveQMax(arr, N, queries, Q);
    return 0;
}


Java




// Java code to implement above approach.
import java.io.*;
import java.util.*;
 
class GFG {
    // Function to find the number
    // of missing element for each query
    public static ArrayList<Integer>
    SolveQMax(int arr[], int n, int queries[][], int q)
    {
        // Initializing an hashmap
        HashMap<Integer, Boolean> visited = new HashMap<>();
        int maxElement;
        ArrayList<Integer> res = new ArrayList<>();
 
        // Loop to find number of missing elements
        for (int i = 0; i < q; i++) {
            maxElement = Integer.MIN_VALUE;
            visited.clear();
            for (int j = queries[i][0]; j <= queries[i][1];
                 j++) {
 
                // Finding the maximum value
                maxElement = Math.max(maxElement, arr[j]);
                visited.put(arr[j], true);
            }
 
            res.add(maxElement - visited.size());
        }
 
        // Return the answer
        return res;
    }
 
    // Driver Code
    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 }
        };
 
        // Function call
        System.out.print(SolveQMax(arr, N, queries, Q));
    }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Python code to implement above approach.
 
INT_MIN = -2147483647 - 1
 
# Function to find the number
# of missing element for each query
def SolveQMax(arr, n, queries, q):
         
    # Initializing an unordered map
    visited = {}
    maxElement = INT_MIN
    res = []
 
    # Loop to find number of missing elements
    for i in range(q):
        maxElement = INT_MIN
        visited = {}
        for j in range(queries[i][0],queries[i][1]+1):
 
            # Finding the maximum value
            maxElement = max(maxElement,arr[j])
            visited[arr[j]] = 1
 
        # document.write(Object.keys(visited).length)
        res.append(maxElement - len(visited))
 
    # Return the answer
    return res
 
# Driver code
 
N = 5
arr = [8, 6, 7, 7, 7]
Q = 5
queries = [[0, 3], [1, 2],[0, 2], [1, 3],[2, 3]]
 
# Function call
print(SolveQMax(arr, N, queries, Q))
 
# This code is contributed by shinjanpatra


C#




// C# code to implement above approach.
using System;
using System.Collections.Generic;
 
class GFG
{
    // Function to find the number
    // of missing element for each query
    public static List<int> SolveQMax(int[] arr, int n,
                                      int[,] queries, int q)
    {
       
        // Initializing an hashmap
        Dictionary<int, Boolean> visited = new Dictionary<int, bool>();
        int maxElement;
        List<int> res = new List<int>();
 
        // Loop to find number of missing elements
        for (int i = 0; i < q; i++)
        {
            maxElement = int.MinValue;
            visited.Clear();
            for (int j = queries[i, 0]; j <= queries[i, 1];
                 j++)
            {
 
                // Finding the maximum value
                maxElement = Math.Max(maxElement, arr[j]);
                visited[arr[j]] = true;
            }
 
            res.Add(maxElement - visited.Count);
        }
 
        // Return the answer
        return res;
    }
 
    // Driver Code
    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 }
        };
 
        // Function call
        List<int> res = SolveQMax(arr, N, queries, Q);
 
        for (int i = 0; i < res.Count; i++)
        {
            Console.Write(res[i] + " ");
        }
    }
}
 
// This code is contributed by Saurbah Jaiswal


Javascript




<script>
    // JavaScript code to implement above approach.
    const INT_MIN = -2147483647 - 1;
 
    // Function to find the number
    // of missing element for each query
    const SolveQMax = (arr, n, queries, q) => {
     
        // Initializing an unordered map
        let visited = {};
        let maxElement = INT_MIN;
        let res = [];
 
        // Loop to find number of missing elements
        for (let i = 0; i < q; i++) {
            maxElement = INT_MIN;
            visited = {};
            for (let j = queries[i][0];
                j <= queries[i][1]; j++) {
 
                // Finding the maximum value
                maxElement = Math.max(maxElement,
                    arr[j]);
                visited[arr[j]] = 1;
            }
            // document.write(Object.keys(visited).length)
            res.push(maxElement
                - Object.keys(visited).length);
        }
 
        // Return the answer
        return res
    }
 
    // Driver code
 
    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]];
 
    // Function call
    document.write(SolveQMax(arr, N, queries, Q));
 
// This code is contributed by rakeshsahni
 
</script>


Output

5 5 5 5 6 

Time Complexity: O(Q * N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads