Open In App

Construct N sized Array such that every K sized Subarray has MEX value D

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to implement the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to construct the array
vector<int> construct(int N, int K, int D)
{
    vector<int> ans;
 
    // if D exceeds K
    if (D > K) {
        ans.push_back(-1);
    }
    else {
 
        // Ptr a is used to include all
        // elements from 0 to D-1
        int a = 0;
        for (int i = 0; i < K; i++) {
            if (a != D) {
                ans.push_back(a);
                a++;
            }
            else {
 
                // If a == D, to skip it
                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;
 
    // Function call
    vector<int> res = construct(N, K, D);
    for (int x : res)
        cout << x << " ";
    return 0;
}


Java




// Java code to implement the above approach
import java.io.*;
import java.util.*;
 
class GFG {
    // Function to construct the array
    public static ArrayList<Integer> construct(int N, int K,
                                               int D)
    {
        ArrayList<Integer> ans = new ArrayList<Integer>();
 
        // if D exceeds K
        if (D > K) {
            ans.add(-1);
        }
        else {
 
            // Ptr a is used to include all
            // elements from 0 to D-1
            int a = 0;
            for (int i = 0; i < K; i++) {
                if (a != D) {
                    ans.add(a);
                    a++;
                }
                else {
 
                    // If a == D, to skip it
                    a++;
                    ans.add(a);
                    a++;
                }
            }
            for (int i = K; i < N; i++) {
                ans.add(ans.get(i - K));
            }
        }
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 4, K = 3, D = 3;
 
        // Function call
        ArrayList<Integer> res = construct(N, K, D);
        for (Integer x : res)
            System.out.print(x + " ");
    }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Python3 implementation of the approach
 
# Function to construct the array
def construct(N, K, D) :
     
    ans = []
 
    # if D exceeds K
    if (D > K) :
        ans.append(-1)
     
    else :
 
        # Ptr a is used to include all
        # elements from 0 to D-1
        a = 0
        for i in range(K):
            if (a != D) :
                ans.append(a)
                a += 1
             
            else :
 
                # If a == D, to skip it
                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
 
    # Function call
    res = construct(N, K, D)
    for x in res :
        print(x, end=" ")
 
# This code is contributed by sanjoy_62.


C#




// C# code to implement the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to construct the array
  public static List<int> construct(int N, int K,
                                    int D)
  {
    List<int> ans = new List<int>();
 
    // if D exceeds K
    if (D > K) {
      ans.Add(-1);
    }
    else {
 
      // Ptr a is used to include all
      // elements from 0 to D-1
      int a = 0;
      for (int i = 0; i < K; i++) {
        if (a != D) {
          ans.Add(a);
          a++;
        }
        else {
 
          // If a == D, to skip it
          a++;
          ans.Add(a);
          a++;
        }
      }
      for (int i = K; i < N; i++) {
        ans.Add(ans[i - K]);
      }
    }
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 4, K = 3, D = 3;
 
    // Function call
    List<int> res = construct(N, K, D);
    foreach (int x in res)
      Console.Write(x + " ");
  }
}
 
// This code is contributed by code_hunt.


Javascript




<script>
 // JavaScript code for the above approach
 
// Function to construct the array
function construct( N, K, D)
{
    let ans =[];
 
    // if D exceeds K
    if (D > K) {
        ans.push(-1);
    }
    else {
 
        // Ptr a is used to include all
        // elements from 0 to D-1
        let a = 0;
        for (let i = 0; i < K; i++) {
            if (a != D) {
                ans.push(a);
                a++;
            }
            else {
 
                // If a == D, to skip it
                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;
 
    // Function call
    let res = construct(N, K, D);
    for (let x of res)
        document.write(x+" ");
    
 
 // This code is contributed by Potta Lokesh
  
</script>


Output

0 1 2 0 

Time Complexity: O(N)
Auxiliary Space: O(N)



Last Updated : 21 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads