MEX of Every Sliding Window

You are given an integer array nums of size n and an integer k.

For every contiguous subarray of size k, moving from left to right, find its mex.

The mex of an array is the smallest non-negative integer that does not appear in that array.

Return an array containing the mex of each window in order.

A window of size k means any contiguous subarray nums[i...i+k-1].

Example 1:

Input: nums = [0, 1, 2, 4, 1], k = 3

Output: [3, 0, 0]

Explanation:

There are 3 windows of size 3:

  • [0, 1, 2] → 0, 1, 2 are present, so the mex is 3
  • [1, 2, 4] → 0 is missing, so the mex is 0
  • [2, 4, 1] → 0 is missing, so the mex is 0

So, the answer is [3, 0, 0].

Example 2:

Input: nums = [1, 2, 0, 4, 3, 1], k = 4

Output: [3, 1, 2]

Explanation:

  • [1, 2, 0, 4] → 0, 1, 2 are present, so mex = 3
  • [2, 0, 4, 3] → 1 is missing, so mex = 1
  • [0, 4, 3, 1] → 2 is missing, so mex = 2

Hence, the answer is [3, 1, 2].

Now Your Turn!

Pick the correct output for the given input

Input: nums = [0, 2, 1, 4], k = 3

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= k <= n <= 2 * 105
  • 0 <= nums[i] <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
vector<int> slidingWindowMex(vector<int>& nums, int k) {
// Your code goes here
}
};
 
Test Case

Input:

K
Nums