Given an integer** N**, the task is to make an array of unique elements of length **N** such that all subarrays sum modulo **N** equals to zero.

**Examples:**

Input:N = 6

Output:6 12 18 24 30 36

Explanation:

Since all elements are a muliple of 6. Hence all subarrays add up to a sum divisible by 6.

Input:N = 4

Output:4 8 12 16

**Approach:**

We can observe that for all subarrays to be divisible by **N**, the elements of the array need to be a multiple of **N**.

**Illustration:**

For

N = 4, if we consider the array elements to {4, 8, 12, 16}, All possible subarrays are:

{4}, {8}, {12}, {16}, {4, 8}, {8, 12}, {12, 16}, {4, 8, 12}, {8, 12, 16}, {4, 8, 12, 16}

Hence, all subarrays have a sum divisible by N.

Hence, to solve the problem, we just need to print **{N, 2*N, 3*N, ….., N*N}** to get the desired array.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the ` `// above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the required ` `// array ` `void` `makeArray(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Print the array ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `cout << i * n << ` `" "` `; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `int` `N = 6; ` ` ` `int` `arr[N]; ` ` ` `makeArray(arr, N); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `// Function to print the required ` `// array ` `static` `void` `makeArray(` `int` `a[], ` `int` `n) ` `{ ` ` ` ` ` `// Print the array ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `System.out.print(i * n + ` `" "` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `6` `; ` ` ` `int` `arr[] = ` `new` `int` `[N]; ` ` ` ` ` `makeArray(arr, N); ` `} ` `} ` ` ` `// This code is contributed by Pratima Pandey ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the ` `# above approach ` ` ` `# Function to print the ` `# required array ` `def` `makeArray(n): ` ` ` ` ` `# Print Array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `print` `((i ` `+` `1` `) ` `*` `n, end ` `=` `" "` `) ` ` ` ` ` `# Driver code ` `n ` `=` `6` `; ` `makeArray(n); ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to print the required ` `// array ` `static` `void` `makeArray(` `int` `[]a, ` `int` `n) ` `{ ` ` ` ` ` `// Print the array ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `Console.Write(i * n + ` `" "` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 6; ` ` ` `int` `[]arr = ` `new` `int` `[N]; ` ` ` ` ` `makeArray(arr, N); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

6 12 18 24 30 36

**Time Complexity:** O(N)

**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Maximum length L such that the sum of all subarrays of length L is less than K
- Count of subarrays of an Array having all unique digits
- Generate all unique partitions of an integer
- Find Sum of all unique sub-array sum for a given array.
- Make sum of all subarrays of length K equal by only inserting elements
- Generate all possible permutations of a Number divisible by N
- To Generate a One Time Password or Unique Identification URL
- Queries to find maximum sum contiguous subarrays of given length in a rotating array
- Count of longest possible subarrays with sum not divisible by K
- Minimum length subarray containing all unique elements after Q operations
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Subsequence pair from given Array having all unique and all same elements respectively
- Unique subsequences of length K with given sum
- Find sum of all unique elements in the array for K queries
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Maximum GCD of all subarrays of length at least 2
- Minimum LCM of all subarrays of length at least 2
- Maximum sum of non-overlapping subarrays of length atmost K
- Count of subarrays having sum equal to its length

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.