Given an integer N, the task is to group numbers such that each group is mutually co-prime together with the total grouping is minimum.
Input: N = 8
1 2 3
Input: N = 5
1 2 3
Approach: The key observation in this problem is two consecutive numbers are always co-prime. That is GCD(a, a+1) = 1. Another important observation is even numbers can’t be listed in one group. Because they will lead to the greatest common divisor of 2. Therefore, every consecutive even and odd numbers can be grouped into one group and 1 can be in any group because the greatest common divisor of numbers with 1 is always 1.
Below is the implementation of the above approach :
1 2 3 4 5 6 7 8 9
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find sum of N-th group of Natural Numbers
- Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)
- Composite XOR and Coprime AND
- Largest Coprime Set Between two integers
- Largest number less than or equal to N/2 which is coprime to N
- Print all distinct Coprime sets possible from 1 to N
- Print all Coprime path of a Binary Tree
- Finding a Non Transitive Coprime Triplet in a Range
- Partition first N natural number into two sets such that their sum is not coprime
- Count all pairs of divisors of a number N whose sum is coprime with N
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- SQL | GROUP BY
- Total ways of selecting a group of X men from N men with or without including a particular man
- Count of groups among N people having only one leader in each group
- Ways of selecting men and women from a group to make a team
- Ways of dividing a group into two halves such that two elements are in different groups
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Number Theory | Generators of finite cyclic group under addition
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
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.
Improved By : sapnasingh4991