Skip to content
Related Articles

Related Articles

Improve Article

Generate a unique Array of length N with sum of all subarrays divisible by N

  • Last Updated : 16 Aug, 2021
Geek Week

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 multiple 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);
}

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

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);

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

Javascript




<script>
    // javascript program for the above approach
   
// Function to print the required
// array
 
function makeArray(n)
{
       
    // Print the array
    for(var i = 1; i <= n; i++)
    document.write(i * n + " ");
}
   
// Driver code
  
    var N = 6;
       
    makeArray(N);
 
   
 
</script>
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :