Given three integers N, K and X. The task is to construct an array of size N where MEX of every K sized subarray is X. Print the constructed array elements, if not possible print -1.
Examples:
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.
Approach: The solution to the problem is based on simple observation. If X is at most (K+1) till then the array can be built. Otherwise, it cannot be built. Follow the steps mentioned below to solve the problem:
- Initialize an array nums[] of size N.
- If value of X is greater than K+1, the array is not possible.
- Fill first K indices starting from 1 and increase the value by 1 every time for the other indices. Skip the value of X when it is to be filled.
- After that fill rest of the indices i.e from K ≤ i ≤ N with value nums[i – K].
- The nums[] array is the required array.
Below is the implementation of the above approach:
// C++ code to implement the above approach #include <bits/stdc++.h> 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 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);
}
} |
# 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# 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. |
<script> // JavaScript code for the above approach
// Function to create array of size N
function MEXSubarray(N, K, X) {
// If X > K+1 then it is not
// possible to create such array
if (X > K + 1)
document.write(-1 + '<br>' )
// Creating array nums of size N
else {
let val = 1;
// Initializing nums
let nums = new Array(N).fill(0);
// Filling first K indices
for (let 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 (let i = K; i < N; i++)
nums[i] = nums[i - K];
// Printing Array
for (let v of nums)
document.write(v + " " );
document.write( '<br>' )
}
}
// Driver Code
let N = 5;
let K = 4;
let X = 5;
MEXSubarray(N, K, X);
// This code is contributed by Potta Lokesh
</script>
|
1 2 3 4 1
Time complexity: O(N)
Auxiliary Space: O(N)