生成一个 N 大小的数组,其中每个 K 大小的子数组的 MEX 为 X
给定三个整数N 、 K和X 。任务是构造一个大小为N的数组,其中每个K大小的子数组的MEX为X 。打印构造的数组元素,如果不可能打印-1。
例子:
Input: N = 4, K = 3, X = 2
Output: 1 3 4 1
Explanation: Subarray of size K = 3 are: {1, 3, 4} and {3, 4, 1} and MEX of both the arrays are X = 2.
Input: N = 5, K = 4, X = 5
Output: 1 2 3 4 1
Explanation: Subarray of size K = 4 are: {1, 2, 3, 4} and {2, 3, 4, 1} and MEX of both the array is X = 5.
方法:问题的解决方法是基于简单的观察。如果 X 最多为 (K+1),则可以构建数组。否则无法构建。请按照以下步骤解决问题:
- 初始化一个大小为N的数组nums[] 。
- 如果X的值大于K+1,则该数组是不可能的。
- 从1开始填充前K个索引,并为其他索引每次将值增加1 。当要填充时跳过X的值。
- 之后用值nums[i – K]填充其余的索引,即从K ≤ i ≤ N 。
- nums[]数组是必需的数组。
下面是上述方法的实现:
C++
// C++ code to implement the above approach
#include
using namespace std;
// Function to create array of size N
void MEXSubarray(int N, int K, int X)
{
// If X > K+1 then it is not
// possible to create such array
if (X > K + 1)
cout << -1 << endl;
// Creating array nums of size N
else {
int val = 1;
// Initializing nums
int nums[N] = { 0 };
// Filling first K indices
for (int i = 0; i < K; i++) {
// Creating MEX in K
// sized subarray
if (val == X)
nums[i] = ++val;
else
nums[i] = val;
val++;
}
// Filling rest of the indices
for (int i = K; i < N; i++)
nums[i] = nums[i - K];
// Printing Array
for (int v : nums)
cout << v << " ";
cout << endl;
}
}
// Driver Code
int main()
{
int N = 5;
int K = 4;
int X = 5;
MEXSubarray(N, K, X);
return 0;
}
Java
// Java code to implement the above approach
import java.util.*;
class GFG {
// Function to create array of size N
public static void MEXSubarray(int N, int K,
int X)
{
// If X > K+1 then it is not
// possible to create such array
if (X > K + 1)
System.out.println(-1);
// Creating array nums of size N
else {
int val = 1;
// Initializing nums
int[] nums = new int[N];
// Filling first K indices
for (int i = 0; i < K; i++) {
// Creating MEX in K
// sized subarray
if (val == X)
nums[i] = ++val;
else
nums[i] = val;
val++;
}
// Filling rest of the indices
for (int i = K; i < N; i++)
nums[i] = nums[i - K];
// Printing Array
for (int v : nums)
System.out.print(v + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
int K = 4;
int X = 5;
MEXSubarray(N, K, X);
}
}
Python3
# Python code to implement the above approach
# Function to create array of size N
def MEXSubarray(N, K, X):
# If X > K+1 then it is not
# possible to create such array
if (X > K + 1):
print(-1)
# Creating array nums of size N
else:
val = 1
# Initializing nums
nums = [0]*N
# Filling first K indices
for i in range(0, K):
# Creating MEX in K
# sized subarray
if (val == X):
nums[i] = ++val
else:
nums[i] = val
val = val + 1
# Filling rest of the indices
for i in range(K, N):
nums[i] = nums[i - K]
# Printing Array
for v in range(len(nums)):
print(nums[v], end=" ")
# Driver Code
N = 5
K = 4
X = 5
MEXSubarray(N, K, X)
# This code is contributed by Taranpreet
C#
// C# code to implement the above approach
using System;
class GFG {
// Function to create array of size N
public static void MEXSubarray(int N, int K, int X)
{
// If X > K+1 then it is not
// possible to create such array
if (X > K + 1)
Console.WriteLine(-1);
// Creating array nums of size N
else {
int val = 1;
// Initializing nums
int[] nums = new int[N];
// Filling first K indices
for (int i = 0; i < K; i++) {
// Creating MEX in K
// sized subarray
if (val == X)
nums[i] = ++val;
else
nums[i] = val;
val++;
}
// Filling rest of the indices
for (int i = K; i < N; i++)
nums[i] = nums[i - K];
// Printing Array
foreach(int v in nums)
Console.Write(v + " ");
}
}
// Driver Code
public static void Main(string[] args)
{
int N = 5;
int K = 4;
int X = 5;
MEXSubarray(N, K, X);
}
}
// This code is contributed by ukasp.
Javascript
输出
1 2 3 4 1
时间复杂度: 在)
辅助空间: 在)