Given an integer N, for every integer i in the range 2 to N, assign a positive integer such that the following conditions hold :
A pair of integers is called coprime if their gcd(i, j) = 1.
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:
1 2 1 3 2
Time Complexity: O(N Log N)
- Generate an array B from the given array A which satisfies the given conditions
- Generate an array using given conditions from a given array
- Generate an array of size K which satisfies the given conditions
- Generate a string from an array of alphanumeric strings based on given conditions
- Generate N integers satisfying the given conditions
- Permute the elements of an array following given order
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Maximum modified Array sum possible by choosing elements as per the given conditions
- Number of K's such that the given array can be divided into two sets satisfying 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
- Construct a binary string following the given constraints
- Print N distinct numbers following the given operations
- Maximum Sum possible by selecting X elements from a Matrix based on given conditions
- Generate original array from an array that store the counts of greater elements on right
- Find an integer in the given range that satisfies the given conditions
- Change the given string according to the given conditions
- Count valid pairs in the array satisfying given conditions
- Split the array into equal sum parts according to given conditions
- Maximum length sub-array which satisfies the given conditions
- Count of operations required to update the array such that it satisfies the given conditions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.