Generate Array whose sum of all K-size subarrays divided by N leaves remainder X

Given three integer N, K and X, the task is to create an array of length N suach that sum of all its K-length subarrays modulo N is X.

Examples:

Input: N = 6, K = 3, X = 3
Output: 9 6 6 9 6 6
Explanation:
All subarrays of length 3 and their respective sum%N values are as follows:
[9, 6, 6] Sum = 21 % 6 = 3
[6, 6, 9] sum = 21 % 6 = 3
[6, 9, 6] sum = 21 % 6 = 3
[9, 6, 6] sum = 21 % 6 = 3
Since all its subarrays have sum % N = X (=3), the generated array is valid.

Input: N = 4, K = 2, X = 2
Output: 6 4 6 4

Approach:
We can observe that in order to make the sum of any subarray of size K modulo N to be equal to X, the subarray needs to have a K – 1 elements equal to N and 1 element equal to N + X.



Illustration:

If N = 6, K = 3, X = 3
Here a K length subarray needs to be a permutation of {9, 6, 6} where 2 (K – 1) elements are divisible by 6 and 1 element has modulo N equal to X( 9%6 = 3)
Sum of subarray % N = (21 % 6) = 3 (same as X)
Hence, each K length

Hence, follow the steps below to solve the problem:

  • Iterate i from 0 to N – 1, to print the ith element of the required subarray.
  • If i % K is equal to 0, print N + X. Otherwise, for all other values of i, print N.
  • This ensures that every possible K-length subarray has a sum K*N + X. Hence sum modulo N is X for all such subarrays.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the
// above approach
#include <bits/stdc++.h>
using namespace std;
  
// Funtion prints the required array
void createArray(int n, int k, int x)
{
    for (int i = 0; i < n; i++) {
        // First element of each K
        // length subarrays
        if (i % k == 0) {
            cout << x + n << " ";
        }
        else {
            cout << n << " ";
        }
    }
}
  
// Driver Program
int main()
{
  
    int N = 6, K = 3, X = 3;
    createArray(N, K, X);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
import java.util.*;
class GFG{
  
// Function prints the required array
static void createArray(int n, int k, int x)
{
    for(int i = 0; i < n; i++)
    {
          
       // First element of each K
       // length subarrays
       if (i % k == 0
       {
           System.out.print((x + n) + " ");
       }
       else
       {
           System.out.print(n + " ");
       }
    }
}
  
// Driver Code
public static void main(String args[])
{
    int N = 6, K = 3, X = 3;
      
    createArray(N, K, X);
}
}
  
// This code is contributed by Code_Mech

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the 
# above approach 
  
# Funtion prints the required array 
def createArray(n, k, x):
      
    for i in range(n):
          
        # First element of each K
        # length subarrays
        if (i % k == 0):
            print(x + n, end = " ")
        else :
            print(n, end = " ")
  
# Driver code
N = 6
K = 3
X = 3
  
createArray(N, K, X)
  
# This code is contributed by Vishal Maurya.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
class GFG{
  
// Function prints the required array
static void createArray(int n, int k, int x)
{
    for(int i = 0; i < n; i++)
    {
         
       // First element of each K
       // length subarrays
       if (i % k == 0) 
       {
           Console.Write((x + n) + " ");
       }
       else
       {
           Console.Write(n + " ");
       }
    }
}
  
// Driver Code
public static void Main()
{
    int N = 6, K = 3, X = 3;
      
    createArray(N, K, X);
}
}
  
// This code is contributed by Code_Mech

chevron_right


Output:

9 6 6 9 6 6

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

competitive-programming-img




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vishu2908, Code_Mech