Construct N sized Array such that every K sized Subarray has MEX value D
Last Updated :
21 Sep, 2022
Given three integers N, K and D, the task is to construct an array of size N such that every subarray of size K has a MEX value of D. If there is no possible answer, output -1.
MEX of an array is the first non-negative integer that is not present in the array.
Examples:
Input: N = 4, K = 3, D = 4
Output: -1
Explanation: As D exceeds K, it is impossible to generate desired array.
Input: N = 4, K = 3, D = 3
Output: 0 1 2 0
Explanation: All subarray of size 3 i.e., {0, 1, 2}, {1, 2, 0} has mex value 3.
Input: N = 4, K = 3, D = 2
Output: 0 1 0 1
Explanation: All subarray of size 3 i.e., {0, 1, 0}, {1, 0, 1} has mex value 2.
Approach: We need to follow a constructive approach in order to solve the above problem.
Check whether D exceeds K, If so then print -1.
Else we can construct the solution:
- Take first K elements of desired array including from 0 to D-1, then including elements after D onwards.
- For remaining next K positions we need to print above elements in cyclic order.
- Repeat the above process until desired length is not achieved.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > construct( int N, int K, int D)
{
vector< int > ans;
if (D > K) {
ans.push_back(-1);
}
else {
int a = 0;
for ( int i = 0; i < K; i++) {
if (a != D) {
ans.push_back(a);
a++;
}
else {
a++;
ans.push_back(a);
a++;
}
}
for ( int i = K; i < N; i++) {
ans.push_back(ans[i - K]);
}
}
return ans;
}
int main()
{
int N = 4, K = 3, D = 3;
vector< int > res = construct(N, K, D);
for ( int x : res)
cout << x << " " ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static ArrayList<Integer> construct( int N, int K,
int D)
{
ArrayList<Integer> ans = new ArrayList<Integer>();
if (D > K) {
ans.add(- 1 );
}
else {
int a = 0 ;
for ( int i = 0 ; i < K; i++) {
if (a != D) {
ans.add(a);
a++;
}
else {
a++;
ans.add(a);
a++;
}
}
for ( int i = K; i < N; i++) {
ans.add(ans.get(i - K));
}
}
return ans;
}
public static void main(String[] args)
{
int N = 4 , K = 3 , D = 3 ;
ArrayList<Integer> res = construct(N, K, D);
for (Integer x : res)
System.out.print(x + " " );
}
}
|
Python3
def construct(N, K, D) :
ans = []
if (D > K) :
ans.append( - 1 )
else :
a = 0
for i in range (K):
if (a ! = D) :
ans.append(a)
a + = 1
else :
a + = 1
ans.append(a)
a + = 1
for i in range (K, N):
ans.append(ans[i - K])
return ans
if __name__ = = "__main__" :
N = 4
K = 3
D = 3
res = construct(N, K, D)
for x in res :
print (x, end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static List< int > construct( int N, int K,
int D)
{
List< int > ans = new List< int >();
if (D > K) {
ans.Add(-1);
}
else {
int a = 0;
for ( int i = 0; i < K; i++) {
if (a != D) {
ans.Add(a);
a++;
}
else {
a++;
ans.Add(a);
a++;
}
}
for ( int i = K; i < N; i++) {
ans.Add(ans[i - K]);
}
}
return ans;
}
public static void Main()
{
int N = 4, K = 3, D = 3;
List< int > res = construct(N, K, D);
foreach ( int x in res)
Console.Write(x + " " );
}
}
|
Javascript
<script>
function construct( N, K, D)
{
let ans =[];
if (D > K) {
ans.push(-1);
}
else {
let a = 0;
for (let i = 0; i < K; i++) {
if (a != D) {
ans.push(a);
a++;
}
else {
a++;
ans.push(a);
a++;
}
}
for (let i = K; i < N; i++) {
ans.push(ans[i - K]);
}
}
return ans;
}
let N = 4, K = 3, D = 3;
let res = construct(N, K, D);
for (let x of res)
document.write(x+ " " );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...