Given an array arr[] consisting of N distinct integers and an integer K, the task is to find the maximum MEX from all subarrays of length K.
The MEX is the smallest positive integer that is not present in the array.
Examples:
Input: arr[] = {3, 2, 1, 4}, K = 2
Output: 3
Explanation:
All subarrays having length 2 are {3, 2}, {2, 1}, {1, 4}.
In subarray {3, 2}, the smallest positive integer which is not present is 1.
In subarray {2, 1}, the smallest positive integer which is not present is 3.
In subarray {1, 4}, the smallest positive integer which is not present is 2.
Input: arr[] = {6, 1, 3, 2, 4}, K = 3
Output: 4
Explanation:
All subarrays having length 3 are {6, 1, 3}, {1, 3, 2}, {3, 2, 4}
In subarray {6, 1, 3}, the smallest positive integer which is not present is 2.
In subarray {1, 3, 2}, the smallest positive integer which is not present is 4.
In subarray {3, 2, 4}, the smallest positive integer which is not present is 1.
Naive Approach: The simplest approach is to generate all subarrays of length K and find MEX of every subarray. After finding all the MEX, print the maximum of those obtained.
Time Complexity: O(K * N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use the data structure Set and Sliding Window Technique. Follow the steps below to solve the problem:
- Initialize a set S to store values that are not present in the current subarray and initially insert 1 to N + 1 number in it because initially, the size of the window is 0.
- Traverse over the range [0, K – 1] and erase arr[i] from the set, the first element of the set is MEX of subarray starting from index 0 and length K, initialize a variable mex and store this value in mex.
- Now iterate from K to N – 1 and erase arr[i] to set and insert arr[i – K] from it and update mex = max(mex, first element of set).
- After the above steps, print mex as maximum MEX among subarray having length K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxMEX( 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 = max(mex, firstElem);
}
cout << mex << ' ' ;
}
int main()
{
int arr[] = { 3, 2, 1, 4 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
maxMEX(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void maxMEX( int arr[], int N, int k)
{
TreeSet<Integer> s = new TreeSet<>();
for ( int l= 1 ;l<=N+ 1 ;l++)
s.add(l);
int i= 0 ;
int j= 0 ;
int mex = 0 ;
int maxMex = Integer.MIN_VALUE;
while (j < N)
{
if (s.contains(arr[j]))
s.remove(arr[j]);
int windowSize = j-i+ 1 ;
if (windowSize < k)
j++;
else if (windowSize == k)
{
mex = s.pollFirst();
maxMex = Math.max(maxMex,mex);
s.add(arr[i]);
i++;
j++;
}
}
System.out.println(maxMex);
}
public static void main(String[] args)
{
int arr[] = { 6 , 1 , 3 , 2 , 4 };
int K = 3 ;
int N = arr.length;
maxMEX(arr, N, K);
}
}
|
Python3
def maxMEX(arr, N, K):
s = set ()
for i in range ( 1 , N + 2 ):
s.add(i)
for i in range (K):
s.remove(arr[i])
mex = list (s)[ 0 ]
for i in range (K, N):
s.remove(arr[i])
s.add(arr[i - K])
firstElem = list (s)[ 0 ]
mex = max (mex, firstElem)
print (mex)
if __name__ = = '__main__' :
arr = [ 3 , 2 , 1 , 4 ]
N = len (arr)
K = 2
maxMEX(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void maxMEX( 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]);
List< int > v = new List< int >();
foreach ( int i in s) { v.Add(i); }
int mex = v[0];
for ( int i = K; i < N; i++)
{
v.Remove(arr[i]);
v.Add(arr[i - K]);
int firstElem = v[0];
mex = Math.Max(mex, firstElem);
}
Console.Write(mex - 2 + " " );
}
public static void Main(String[] args)
{
int [] arr = { 3, 2, 1, 4 };
int K = 2;
int N = arr.Length;
maxMEX(arr, N, K);
}
}
|
Javascript
<script>
function maxMEX( 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 a = Array.from(s);
var mex = a[0];
for (let i = K; i < N; i++) {
s. delete (arr[i]);
s.add(arr[i - K]);
let ss = Array.from(s);
var firstElem = ss[ss.length-1];
mex = Math.max(mex, firstElem);
}
document.write( mex , ' ' );
}
let arr = [ 3, 2, 1, 4 ];
let K = 2;
let N = arr.length;
maxMEX(arr, N, K);
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)