Generate a N size Array where MEX of every K sized Subarray is X
Last Updated :
07 Mar, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void MEXSubarray( int N, int K, int X)
{
if (X > K + 1)
cout << -1 << endl;
else {
int val = 1;
int nums[N] = { 0 };
for ( int i = 0; i < K; i++) {
if (val == X)
nums[i] = ++val;
else
nums[i] = val;
val++;
}
for ( int i = K; i < N; i++)
nums[i] = nums[i - K];
for ( int v : nums)
cout << v << " " ;
cout << endl;
}
}
int main()
{
int N = 5;
int K = 4;
int X = 5;
MEXSubarray(N, K, X);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void MEXSubarray( int N, int K,
int X)
{
if (X > K + 1 )
System.out.println(- 1 );
else {
int val = 1 ;
int [] nums = new int [N];
for ( int i = 0 ; i < K; i++) {
if (val == X)
nums[i] = ++val;
else
nums[i] = val;
val++;
}
for ( int i = K; i < N; i++)
nums[i] = nums[i - K];
for ( int v : nums)
System.out.print(v + " " );
}
}
public static void main(String[] args)
{
int N = 5 ;
int K = 4 ;
int X = 5 ;
MEXSubarray(N, K, X);
}
}
|
Python3
def MEXSubarray(N, K, X):
if (X > K + 1 ):
print ( - 1 )
else :
val = 1
nums = [ 0 ] * N
for i in range ( 0 , K):
if (val = = X):
nums[i] = + + val
else :
nums[i] = val
val = val + 1
for i in range (K, N):
nums[i] = nums[i - K]
for v in range ( len (nums)):
print (nums[v], end = " " )
N = 5
K = 4
X = 5
MEXSubarray(N, K, X)
|
C#
using System;
class GFG {
public static void MEXSubarray( int N, int K, int X)
{
if (X > K + 1)
Console.WriteLine(-1);
else {
int val = 1;
int [] nums = new int [N];
for ( int i = 0; i < K; i++) {
if (val == X)
nums[i] = ++val;
else
nums[i] = val;
val++;
}
for ( int i = K; i < N; i++)
nums[i] = nums[i - K];
foreach ( int v in nums)
Console.Write(v + " " );
}
}
public static void Main( string [] args)
{
int N = 5;
int K = 4;
int X = 5;
MEXSubarray(N, K, X);
}
}
|
Javascript
<script>
function MEXSubarray(N, K, X) {
if (X > K + 1)
document.write(-1 + '<br>' )
else {
let val = 1;
let nums = new Array(N).fill(0);
for (let i = 0; i < K; i++) {
if (val == X)
nums[i] = ++val;
else
nums[i] = val;
val++;
}
for (let i = K; i < N; i++)
nums[i] = nums[i - K];
for (let v of nums)
document.write(v + " " );
document.write( '<br>' )
}
}
let N = 5;
let K = 4;
let X = 5;
MEXSubarray(N, K, X);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...