Minimum MEX from all subarrays of length K
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++
#include <bits/stdc++.h>
using namespace std;
void minimumMEX( int arr[], int N, int K)
{
set< int > s;
for ( int i = 1; i <= N + 1; i++)
s.insert(i);
for ( int i = 0; i < K; i++)
s.erase(arr[i]);
int mex = *(s.begin());
for ( int i = K; i < N; i++) {
s.erase(arr[i]);
s.insert(arr[i - K]);
int firstElem = *(s.begin());
mex = min(mex, firstElem);
}
cout << mex << ' ' ;
}
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
import java.util.HashSet;
class GFG{
static void minimumMEX( int arr[], int N, int K)
{
HashSet<Integer> s = new HashSet<Integer>();
for ( int i = 1 ; i <= N + 1 ; i++)
s.add(i);
for ( int i = 0 ; i < K; i++)
s.remove(arr[i]);
int mex = s.iterator().next();
for ( int i = K; i < N; i++)
{
s.remove(arr[i]);
s.add(arr[i - K]);
int firstElem = s.iterator().next();
mex = Math.min(mex, firstElem);
}
System.out.print(mex + " " );
}
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);
}
}
|
Python3
def minimumMEX(arr, N, K):
s = set ()
for i in range ( 1 , N + 2 , 1 ):
s.add(i)
for i in range (K):
s.remove(arr[i])
mex = list (s)[ 0 ]
for i in range (K,N, 1 ):
s.remove(arr[i])
s.add(arr[i - K])
firstElem = list (s)[ 0 ]
mex = min (mex, firstElem)
print (mex)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
K = 3
N = len (arr)
minimumMEX(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
static void minimumMEX( int [] arr, int N, int K)
{
HashSet< int > s = new HashSet< int >();
for ( int i = 1; i <= N + 1; i++)
s.Add(i);
for ( int i = 0; i < K; i++)
s.Remove(arr[i]);
int mex = s.First();
for ( int i = K; i < N; i++)
{
s.Remove(arr[i]);
s.Add(arr[i - K]);
int firstElem = s.First();
mex = Math.Min(mex, firstElem);
}
Console.Write(mex + " " );
}
static void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int K = 3;
int N = arr.Length;
minimumMEX(arr, N, K);
}
}
|
Javascript
<script>
function minimumMEX(arr, N, K)
{
let s = new Set();
for (let i = 1; i <= N + 1; i++)
s.add(i);
for (let i = 0; i < K; i++)
s. delete (arr[i]);
let entry = s.entries();
let mex = 1;
for (let i = K; i < N; i++)
{
s. delete (arr[i]);
s.add(arr[i - K]);
let firstElem = entry.next().value
mex = Math.min(mex, 1);
}
document.write(mex + " " );
}
let arr = [ 1, 2, 3, 4, 5, 6 ];
let K = 3;
let N = arr.length;
minimumMEX(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
30 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...