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 i^{th} 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; // 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); } |
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 # 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. |
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 |
9 6 6 9 6 6
Time Complexity: O(N)
Auxiliary Space: O(1)
Recommended Posts:
- Find remainder of array multiplication divided by n
- Find if array can be divided into two subarrays of equal sum
- Maximum possible remainder when an element is divided by other element in the array
- Generate a unique Array of length N with sum of all subarrays divisible by N
- Check if the remainder of N-1 factorial when divided by N is N-1 or not
- Program to find remainder when large number is divided by r
- Program to find remainder when large number is divided by 11
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Tree with N nodes and K leaves such that distance between farthest leaves is minimized
- Minimize K whose XOR with given array elements leaves array unchanged
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Maximize array sum by X increments when each element is divided by 10
- Check if an array can be divided into pairs whose sum is divisible by k
- Check if a sorted array can be divided in pairs whose sum is k
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Generate original array from an array that store the counts of greater elements on right
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.