Open In App

Minimum MEX from all subarrays of length K

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] consisting of N distinct positive integers and an integer K, the task is to find the minimum MEX from all subarrays of length K.

The MEX is the smallest positive integer that is not present in the array.

Examples:

Input: arr[] = {1, 2, 3}, K = 2
Output: 1
Explanation:
All subarrays of length 2 are {1, 2}, {2, 3}.
In subarray {1, 2}, the smallest positive integer which is not present is 3.
In subarray {2, 3}, the smallest positive integer which is not present is 1.
Therefore, the minimum of all the MEX for all subarrays of length K (= 2) is 1.

Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 3
Output: 1

Naive Approach: The simplest approach to solve the problem is to generate all subarrays of length K and find the MEX for every subarray. After finding all the MEX, print the minimum of those obtained. 
Time Complexity: O(K * N2)
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized by using a Set and Sliding Window technique. Follow the steps below to solve the problem:

  • Initialize a variable, say mex, to store the minimum among all the MEX of subarrays of size K.
  • Initialize a set S to store values that are not present in the current subarray. Initially insert all numbers from the range [1, N + 1] in it, because initially, the size of the window is 0.
  • Iterate over the range [0, K – 1] and erase the element arr[i] from the set.
  • Now, the first element of the set is MEX of subarray over the range [0, K] and store this value in mex.
  • Now, iterate over the range[K, N – 1] and perform the following steps:
  • After completing the above steps, print the value of mex as the minimum MEX among all the subarrays of size K.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return minimum
// MEX from all K-length subarrays
void minimumMEX(int arr[], int N, int K)
{
    // Stores element from [1, N + 1]
    // which are not present in subarray
    set<int> s;
 
    // Store number 1 to N + 1 in set s
    for (int i = 1; i <= N + 1; i++)
        s.insert(i);
 
    // Find the MEX of K-length
    // subarray starting from index 0
    for (int i = 0; i < K; i++)
        s.erase(arr[i]);
 
    int mex = *(s.begin());
 
    // Find the MEX of all subarrays
    // of length K by erasing arr[i]
    // and inserting arr[i - K]
    for (int i = K; i < N; i++) {
 
        s.erase(arr[i]);
 
        s.insert(arr[i - K]);
 
        // Store first element of set
        int firstElem = *(s.begin());
 
        // Updating the mex
        mex = min(mex, firstElem);
    }
 
    // Print minimum MEX of
    // all K length subarray
    cout << mex << ' ';
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int K = 3;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    minimumMEX(arr, N, K);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.HashSet;
 
class GFG{
     
// Function to return minimum
// MEX from all K-length subarrays
static void minimumMEX(int arr[], int N, int K)
{
     
    // Stores element from [1, N + 1]
    // which are not present in subarray
    HashSet<Integer> s = new HashSet<Integer>();
 
    // Store number 1 to N + 1 in set s
    for(int i = 1; i <= N + 1; i++)
        s.add(i);
 
    // Find the MEX of K-length
    // subarray starting from index 0
    for(int i = 0; i < K; i++)
        s.remove(arr[i]);
 
    int mex = s.iterator().next();
 
    // Find the MEX of all subarrays
    // of length K by erasing arr[i]
    // and inserting arr[i - K]
    for(int i = K; i < N; i++)
    {
        s.remove(arr[i]);
        s.add(arr[i - K]);
 
        // Store first element of set
        int firstElem = s.iterator().next();
 
        // Updating the mex
        mex = Math.min(mex, firstElem);
    }
 
    // Print minimum MEX of
    // all K length subarray
    System.out.print(mex + " ");
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int K = 3;
    int N = arr.length;
 
    minimumMEX(arr, N, K);
}
}
 
// This code is contributed by abhinavjain194


Python3




# Python 3 program for the above approach
 
# Function to return minimum
# MEX from all K-length subarrays
def minimumMEX(arr, N, K):
   
    # Stores element from [1, N + 1]
    # which are not present in subarray
    s = set()
 
    # Store number 1 to N + 1 in set s
    for i in range(1, N + 2, 1):
        s.add(i)
 
    # Find the MEX of K-length
    # subarray starting from index 0
    for i in range(K):
        s.remove(arr[i])
 
    mex = list(s)[0]
 
    # Find the MEX of all subarrays
    # of length K by erasing arr[i]
    # and inserting arr[i - K]
    for i in range(K,N,1):
        s.remove(arr[i])
 
        s.add(arr[i - K])
 
        # Store first element of set
        firstElem = list(s)[0]
 
        # Updating the mex
        mex = min(mex, firstElem)
 
    # Print minimum MEX of
    # all K length subarray
    print(mex)
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5, 6]
    K = 3
    N = len(arr)
    minimumMEX(arr, N, K)
 
    # This code is contributed by ipg2016107.


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG{
     
// Function to return minimum
// MEX from all K-length subarrays
static void minimumMEX(int[] arr, int N, int K)
{
     
    // Stores element from [1, N + 1]
    // which are not present in subarray
    HashSet<int> s = new HashSet<int>();
 
    // Store number 1 to N + 1 in set s
    for(int i = 1; i <= N + 1; i++)
        s.Add(i);
 
    // Find the MEX of K-length
    // subarray starting from index 0
    for(int i = 0; i < K; i++)
        s.Remove(arr[i]);
    int mex = s.First();
 
    // Find the MEX of all subarrays
    // of length K by erasing arr[i]
    // and inserting arr[i - K]
    for(int i = K; i < N; i++)
    {
        s.Remove(arr[i]);
        s.Add(arr[i - K]);
 
        // Store first element of set
        int firstElem = s.First();
 
        // Updating the mex
        mex = Math.Min(mex, firstElem);
    }
 
    // Print minimum MEX of
    // all K length subarray
    Console.Write(mex + " ");
}
 
// Driver code
static void Main()
{
    int[] arr = { 1, 2, 3, 4, 5, 6 };
    int K = 3;
    int N = arr.Length;
 
    minimumMEX(arr, N, K);
}
}
 
// This code is contributed by abhinavjain194


Javascript




<script>
    // Javascript program for the above approach
     
    // Function to return minimum
    // MEX from all K-length subarrays
    function minimumMEX(arr, N, K)
    {
 
        // Stores element from [1, N + 1]
        // which are not present in subarray
        let s = new Set();
 
        // Store number 1 to N + 1 in set s
        for(let i = 1; i <= N + 1; i++)
            s.add(i);
 
        // Find the MEX of K-length
        // subarray starting from index 0
        for(let i = 0; i < K; i++)
            s.delete(arr[i]);
        let entry = s.entries();
        let mex = 1;
 
        // Find the MEX of all subarrays
        // of length K by erasing arr[i]
        // and inserting arr[i - K]
        for(let i = K; i < N; i++)
        {
            s.delete(arr[i]);
            s.add(arr[i - K]);
 
            // Store first element of set
            let firstElem = entry.next().value
 
            // Updating the mex
            mex = Math.min(mex, 1);
        }
 
        // Print minimum MEX of
        // all K length subarray
        document.write(mex + " ");
    }
     
    let arr = [ 1, 2, 3, 4, 5, 6 ];
    let K = 3;
    let N = arr.length;
  
    minimumMEX(arr, N, K);
 
// This code is contributed by divyesh072019.
</script>


Output: 

1

 

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



Last Updated : 30 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads