Open In App

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

Last Updated : 27 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given three integer N, K and X, the task is to create an array of length N such 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++
// C++ implementation of the
// above approach
#include <bits/stdc++.h>
using namespace std;

// Function 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);
}
Java
// 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
Python3
# Python3 implementation of the 
# above approach 

# Function 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.
C#
// 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
JavaScript
<script>

// Javascript implementation of the
// above approach

// Function prints the required array
function createArray(n, k, x)
{
    for (var i = 0; i < n; i++) {
        // First element of each K
        // length subarrays
        if (i % k == 0) {
            document.write( x + n + " ");
        }
        else {
            document.write( n + " ");
        }
    }
}

// Driver Program
var N = 6, K = 3, X = 3;
createArray(N, K, X);

// This code is contributed by itsok.
</script>  

Output: 
9 6 6 9 6 6

 

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


Next Article

Similar Reads