# Generate elements of the array following given conditions

Given an integer N, for every integer **i** in the range **2** to **N**, assign a positive integer such that the following conditions hold :

**(i, j)**, if

**i**and

**j**are coprime then .

**A pair of integers is called coprime if their gcd(i, j) = 1.**

**Examples:**

Input:N = 4

Output:1 2 1

Input:N = 10

Output:1 2 1 3 2 4 1 2 3

**Approach:** We have to keep two things in mind while assigning values at indices from **2** to **n**. First, for any pair **(i, j)**, if **i** and **j** are coprime then we cannot assign same value to this pair of indices. Second, we have to minimize the maximum value assigned to each index from **2** to **n**.

This can be achieved if distinct numbers are assigned to each prime, because all primes are coprime to each other. Then assign values at all composite positions same as any of its prime divisors. This solution works as for any pair **(i, j)**, **i** is given the same number of a divisor and so is **j**, so if they are coprime, they cannot be given the same number.

This approach can be implemented by making a small change when building the Sieve of Eratosthenes.

When we met a prime for the first time, then assign a unique smallest value to it and all of its multiples. This way, all prime indices should have unique values and all composite indices have values same as any of its prime divisors.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to generate the required array ` `void` `specialSieve(` `int` `n) ` `{ ` ` ` ` ` `// Initialize cnt variable for assigning ` ` ` `// unique value to prime and its multiples ` ` ` `int` `cnt = 0; ` ` ` `int` `prime[n + 1]; ` ` ` ` ` `for` `(` `int` `i = 0; i <= n; i++) ` ` ` `prime[i] = 0; ` ` ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` ` ` `// When we get a prime for the first time ` ` ` `// then assign a unique smallest value to ` ` ` `// it and all of its multiples ` ` ` `if` `(!prime[i]) { ` ` ` `cnt++; ` ` ` `for` `(` `int` `j = i; j <= n; j += i) ` ` ` `prime[j] = cnt; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the generated array ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `cout << prime[i] << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 6; ` ` ` ` ` `specialSieve(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to generate the required array ` `static` `void` `specialSieve(` `int` `n) ` `{ ` ` ` ` ` `// Initialize cnt variable for assigning ` ` ` `// unique value to prime and its multiples ` ` ` `int` `cnt = ` `0` `; ` ` ` `int` `prime[] = ` `new` `int` `[n+` `1` `]; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i <= n; i++) ` ` ` `prime[i] = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// When we get a prime for the first time ` ` ` `// then assign a unique smallest value to ` ` ` `// it and all of its multiples ` ` ` `if` `(!(prime[i]>` `0` `)) ` ` ` `{ ` ` ` `cnt++; ` ` ` `for` `(` `int` `j = i; j <= n; j += i) ` ` ` `prime[j] = cnt; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the generated array ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `System.out.print(prime[i] + ` `" "` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `n = ` `6` `; ` ` ` `specialSieve(n); ` ` ` `} ` `} ` ` ` `// This code is contrubuted by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to generate the required array ` `def` `specialSieve(n) : ` ` ` ` ` `# Initialize cnt variable for assigning ` ` ` `# unique value to prime and its multiples ` ` ` `cnt ` `=` `0` `; ` ` ` `prime ` `=` `[` `0` `]` `*` `(n ` `+` `1` `); ` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `) : ` ` ` ` ` `# When we get a prime for the first time ` ` ` `# then assign a unique smallest value to ` ` ` `# it and all of its multiples ` ` ` `if` `(` `not` `prime[i]) : ` ` ` `cnt ` `+` `=` `1` `; ` ` ` `for` `j ` `in` `range` `(i, n ` `+` `1` `, i) : ` ` ` `prime[j] ` `=` `cnt; ` ` ` ` ` `# Print the generated array ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `) : ` ` ` `print` `(prime[i],end ` `=` `" "` `); ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `6` `; ` ` ` `specialSieve(n); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to generate the required array ` `static` `void` `specialSieve(` `int` `n) ` `{ ` ` ` ` ` `// Initialize cnt variable for assigning ` ` ` `// unique value to prime and its multiples ` ` ` `int` `cnt = 0; ` ` ` `int` `[]prime = ` `new` `int` `[n+1]; ` ` ` ` ` `for` `(` `int` `i = 0; i <= n; i++) ` ` ` `prime[i] = 0; ` ` ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// When we get a prime for the first time ` ` ` `// then assign a unique smallest value to ` ` ` `// it and all of its multiples ` ` ` `if` `(!(prime[i] > 0)) ` ` ` `{ ` ` ` `cnt++; ` ` ` `for` `(` `int` `j = i; j <= n; j += i) ` ` ` `prime[j] = cnt; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the generated array ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `Console.Write(prime[i] + ` `" "` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `n = 6; ` ` ` ` ` `specialSieve(n); ` ` ` `} ` `} ` ` ` `// This code is contrubuted by anuj_67.. ` |

*chevron_right*

*filter_none*

**Output:**

1 2 1 3 2

**Time Complexity:** O(N Log N)

## Recommended Posts:

- Generate an array of size K which satisfies the given conditions
- Generate an array of K elements such that sum of elements is N and the condition a[i] < a[i+1] <= 2*a[i] is met | Set 2
- Generate original array from an array that store the counts of greater elements on right
- Generate original array from difference between every two consecutive elements
- Split the array into equal sum parts according to given conditions
- Maximum length sub-array which satisfies the given conditions
- Count valid pairs in the array satisfying given conditions
- Generate minimum sum sequence of integers with even elements greater
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Generate array with minimum sum which can be deleted in P steps
- Generate two BSTs from the given array such that maximum height among them is minimum
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Find original array from encrypted array (An array of sums of other elements)
- Average of remaining elements after removing K largest and K smallest elements from array

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.