给定由N个不同的整数和整数K组成的数组arr [] ,任务是从长度为K的所有子数组中找到最大MEX 。
The MEX is the smallest positive integer that is not present in the array.
例子:
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.
天真的方法:最简单的方法是生成所有长度为K的子数组,并找到每个子数组的MEX 。找到所有MEX后,打印获得的最大MEX 。
时间复杂度: O(K * N 2 )
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是使用数据结构设置和滑动窗口技术。请按照以下步骤解决问题:
- 初始化集合S来存储当前子数组中不存在的值,并在其中初始插入1到N + 1个数字,因为最初窗口的大小为0 。
- 遍历范围[0,K – 1]并从集合中删除arr [i] ,集合的第一个元素是从索引0和长度K开始的子数组的MEX ,初始化变量mex并将此值存储在mex中。
- 现在从K迭代到N – 1,并插入arr [i]进行设置并从中删除arr [i – K]并更新mex = max(mex,集合的第一个元素)。
- 完成上述步骤后,在长度为K的子阵列中将mex打印为最大MEX 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return maximum MEX of
// all K length subarray
void maxMEX(int arr[], int N, int K)
{
// Stores element from 1 to N + 1
// is nor present in subarray
set 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 subarray 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 mex
mex = max(mex, firstElem);
}
// Print maximum MEX of all K
// length subarray
cout << mex << ' ';
}
// Driver Code
int main()
{
// Given array
int arr[] = { 3, 2, 1, 4 };
// Given length of subarray
int K = 2;
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
maxMEX(arr, N, K);
return 0;
}
Java
// Java program for the
// above approach
import java.util.*;
class GFG {
// Function to return maximum
// MEX of all K length subarray
static void maxMEX(int arr[], int N, int K)
{
// Stores element from
// 1 to N + 1 is nor
// present in subarray
// We need a Tree Set since
// we want to store the
// elements in ascending
// order
Set s = new TreeSet<>();
// 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 = 0;
// Extracting first element from set
for (int e : s)
{
mex = e;
break;
}
// Find the MEX of all subarray 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
for (int e : s) {
if (e > mex)
mex = e; // Updating mex
break;
}
}
// Print maximum MEX of all K
// length subarray
System.out.print(mex + " ");
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = { 6, 1, 3, 2, 4 };
// Given length of subarray
int K = 3;
// Size of the array
int N = arr.length;
// Function Call
maxMEX(arr, N, K);
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program for the above approach
# Function to return maximum MEX of
# all K length subarray
def maxMEX(arr, N, K):
# Stores element from 1 to N + 1
# is nor present in subarray
s = set()
# Store number 1 to N + 1 in set s
for i in range(1, N + 2):
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 subarray of
# length K by erasing arr[i]
# and inserting arr[i-K]
for i in range(K, N):
s.remove(arr[i])
s.add(arr[i - K])
# Store first element of set
firstElem = list(s)[0]
# Updating mex
mex = max(mex, firstElem)
# Print maximum MEX of all K
# length subarray
print(mex)
# Driver code
if __name__ == '__main__':
# Given array
arr = [3, 2, 1, 4]
# Size of the array
N = len(arr)
# Given length of subarray
K = 2
# Function Call
maxMEX(arr, N, K)
# This code is contributed by Shivam Singh
C#
// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to return maximum
// MEX of all K length subarray
static void maxMEX(int[] arr, int N, int K)
{
// Stores element from
// 1 to N + 1 is nor
// present in subarray
HashSet s = new HashSet();
// 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]);
List v = new List();
foreach(int i in s) { v.Add(i); }
int mex = v[0];
// Find the MEX of all subarray of
// length K by erasing arr[i]
// and inserting arr[i-K]
for (int i = K; i < N; i++)
{
v.Remove(arr[i]);
v.Add(arr[i - K]);
// Store first element
// of set
int firstElem = v[0];
// Updating mex
mex = Math.Max(mex, firstElem);
}
// Print maximum MEX of all K
// length subarray
Console.Write(mex - 2 + " ");
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int[] arr = { 3, 2, 1, 4 };
// Given length of subarray
int K = 2;
// Size of the array
int N = arr.Length;
// Function Call
maxMEX(arr, N, K);
}
}
// This code is contributed by gauravrajput1
3
时间复杂度: O(N * log N)
辅助空间: O(N)