Given a positive integer **N**, the task is to generate an array such that the sum of Euler Totient Function of each element is equal to **N**.

**Examples:**

Input:N = 6Output:3 8Explanation:

The total number of integers i, from 1 to 3 such that GCD(i, 3) = 1 is 2.

The total number of integers i, from 1 to 6 such that GCD(i, 6) = 1 is 4.

After the above operations, the required array whose sum is 6 is {3, 8}.

Input:N = 12Output:1 12 2 6 3 4

**Approach:** The given problem can be solved based on the divisor sum property of the Euler Totient Function, i.e.,

- The Euler Totient Function of a number
**N**< is the number of integers from**1**to**N**that gives**GCD(i, N)**as**1**and a number**N**can be represented as the summation of the Euler Totient Function values of all the divisors of**N**. - Therefore, the idea is to find the divisors of the given number
**N**as the resultant array.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to construct the array such` `// the sum of values of Euler Totient` `// functions of all array elements is N` `void` `constructArray(` `int` `N)` `{` ` ` `// Stores the resutlant array` ` ` `vector<` `int` `> ans;` ` ` `// Find divisors in sqrt(N)` ` ` `for` `(` `int` `i = 1; i * i <= N; i++) {` ` ` `// If N is divisible by i` ` ` `if` `(N % i == 0) {` ` ` `// Push the current divisor` ` ` `ans.push_back(i);` ` ` `// If N is not a` ` ` `// perfect square` ` ` `if` `(N != (i * i)) {` ` ` `// Push the second divisor` ` ` `ans.push_back(N / i);` ` ` `}` ` ` `}` ` ` `}` ` ` `// Print the resultant array` ` ` `for` `(` `auto` `it : ans) {` ` ` `cout << it << ` `" "` `;` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 12;` ` ` `// Function Call` ` ` `constructArray(N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` ` ` `// Function to construct the array such` `// the sum of values of Euler Totient` `// functions of all array elements is N` `static` `void` `constructArray(` `int` `N)` `{` ` ` ` ` `// Stores the resutlant array` ` ` `ArrayList<Integer> ans = ` `new` `ArrayList<Integer>();` ` ` `// Find divisors in sqrt(N)` ` ` `for` `(` `int` `i = ` `1` `; i * i <= N; i++)` ` ` `{` ` ` ` ` `// If N is divisible by i` ` ` `if` `(N % i == ` `0` `)` ` ` `{` ` ` ` ` `// Push the current divisor` ` ` `ans.add(i);` ` ` `// If N is not a` ` ` `// perfect square` ` ` `if` `(N != (i * i))` ` ` `{` ` ` ` ` `// Push the second divisor` ` ` `ans.add(N / i);` ` ` `}` ` ` `}` ` ` `}` ` ` `// Print the resultant array` ` ` `for` `(` `int` `it : ans)` ` ` `{` ` ` `System.out.print(it + ` `" "` `);` ` ` `}` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `12` `;` ` ` `// Function Call` ` ` `constructArray(N);` `}` `}` `// This code is contributed by splevel62` |

## Python3

`# Python3 program for the above approach` `from` `math ` `import` `sqrt` `# Function to construct the array such` `# the sum of values of Euler Totient` `# functions of all array elements is N` `def` `constructArray(N):` ` ` ` ` `# Stores the resutlant array` ` ` `ans ` `=` `[]` ` ` `# Find divisors in sqrt(N)` ` ` `for` `i ` `in` `range` `(` `1` `, ` `int` `(sqrt(N)) ` `+` `1` `, ` `1` `):` ` ` ` ` `# If N is divisible by i` ` ` `if` `(N ` `%` `i ` `=` `=` `0` `):` ` ` ` ` `# Push the current divisor` ` ` `ans.append(i)` ` ` `# If N is not a` ` ` `# perfect square` ` ` `if` `(N !` `=` `(i ` `*` `i)):` ` ` ` ` `# Push the second divisor` ` ` `ans.append(N ` `/` `i)` ` ` `# Print the resultant array` ` ` `for` `it ` `in` `ans:` ` ` `print` `(` `int` `(it), end ` `=` `" "` `)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `N ` `=` `12` ` ` ` ` `# Function Call` ` ` `constructArray(N)` `# This code is contributed by ipg2016107` |

## C#

`// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG{` ` ` `// Function to construct the array such` `// the sum of values of Euler Totient` `// functions of all array elements is N` `static` `void` `constructArray(` `int` `N)` `{` ` ` ` ` `// Stores the resutlant array` ` ` `List<` `int` `> ans = ` `new` `List<` `int` `>();` ` ` `// Find divisors in sqrt(N)` ` ` `for` `(` `int` `i = 1; i * i <= N; i++)` ` ` `{` ` ` ` ` `// If N is divisible by i` ` ` `if` `(N % i == 0)` ` ` `{` ` ` ` ` `// Push the current divisor` ` ` `ans.Add(i);` ` ` `// If N is not a` ` ` `// perfect square` ` ` `if` `(N != (i * i))` ` ` `{` ` ` ` ` `// Push the second divisor` ` ` `ans.Add(N / i);` ` ` `}` ` ` `}` ` ` `}` ` ` `// Print the resultant array` ` ` `foreach` `(` `int` `it ` `in` `ans)` ` ` `{` ` ` `Console.Write(it + ` `" "` `);` ` ` `}` `}` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` `int` `N = 12;` ` ` `// Function Call` ` ` `constructArray(N);` `}` `}` `// This code is contributed by ukasp` |

**Output:**

1 12 2 6 3 4

**Time Complexity:** O(√N)**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**