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.**Examples:**

Input:N = 8Output:

1 2 3

4 5

6 7

8

Input:N = 5Output:

1 2 3

4 5

**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 :

## C++

`// C++ implementation to group ` `// mutually coprime numbers into ` `// one group with minimum group possible ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to group the mutually ` `// co-prime numbers into one group ` `void` `mutually_coprime(` `int` `n) ` `{ ` ` ` `if` `(n <= 3) ` ` ` `{ ` ` ` ` ` `// Loop for the numbers less ` ` ` `// than the 4 ` ` ` `for` `(` `int` `j = 1; j <= n; j++) ` ` ` `{ ` ` ` `cout << j << ` `" "` `; ` ` ` `} ` ` ` `cout << ` `"\n"` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Integers 1, 2 and 3 can be ` ` ` `// grouped into one group ` ` ` `cout << ` `"1 2 3\n"` `; ` ` ` ` ` `for` `(` `int` `j = 4; j < n; j += 2) ` ` ` `{ ` ` ` ` ` `// Consecutive even and ` ` ` `// odd numbers ` ` ` `cout << j << ` `" "` `<< j + 1 << ` `"\n"` `; ` ` ` `} ` ` ` `if` `(n % 2 == 0) ` ` ` `cout << n << ` `"\n"` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 9; ` ` ` ` ` `// Function call ` ` ` `mutually_coprime(n); ` `} ` ` ` `// This code is contributed by yatinagg ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to group ` `// mutually coprime numbers into ` `// one group with minimum group possible ` `class` `GFG{ ` ` ` `// Function to group the mutually ` `// co-prime numbers into one group ` `static` `void` `mutually_coprime(` `int` `n) ` `{ ` ` ` `if` `(n <= ` `3` `) ` ` ` `{ ` ` ` ` ` `// Loop for the numbers less ` ` ` `// than the 4 ` ` ` `for` `(` `int` `j = ` `1` `; j < n + ` `1` `; j++) ` ` ` `System.out.print(j + ` `" "` `); ` ` ` `System.out.println(); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Integers 1, 2 and 3 can be ` ` ` `// grouped into one group ` ` ` `System.out.println(` `"1 2 3"` `); ` ` ` `for` `(` `int` `j = ` `4` `; j < n; j += ` `2` `) ` ` ` `{ ` ` ` ` ` `// Consecutive even and ` ` ` `// odd numbers ` ` ` `System.out.println(j + ` `" "` `+ (j + ` `1` `)); ` ` ` `if` `(n % ` `2` `== ` `0` `) ` ` ` `System.out.println(n); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `9` `; ` ` ` ` ` `// Function Call ` ` ` `mutually_coprime(n); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to group ` `# mutually coprime numbers into ` `# one group with minimum group possible ` ` ` `# Function to group the mutually ` `# co-prime numbers into one group ` `def` `mutually_coprime (n): ` ` ` `if` `( n <` `=` `3` `): ` ` ` `# Loop for the numbers less ` ` ` `# than the 4 ` ` ` `for` `j ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` `print` `(j, end ` `=` `" "` `) ` ` ` `print` `() ` ` ` `else` `: ` ` ` `# Integers 1, 2 and 3 can be ` ` ` `# grouped into one group ` ` ` `print` `(` `1` `, ` `2` `, ` `3` `) ` ` ` `for` `j ` `in` `range` `( ` `4` `, n, ` `2` `): ` ` ` ` ` `# Consecutive even and ` ` ` `# odd numbers ` ` ` `print` `(j, ( j ` `+` `1` `)) ` ` ` `if` `(n ` `%` `2` `=` `=` `0` `): ` ` ` `print` `(n) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `9` ` ` ` ` `# Function Call ` ` ` `mutually_coprime (n) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to group ` `// mutually coprime numbers into ` `// one group with minimum group possible ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to group the mutually ` `// co-prime numbers into one group ` `static` `void` `mutually_coprime(` `int` `n) ` `{ ` ` ` `if` `(n <= 3) ` ` ` `{ ` ` ` ` ` `// Loop for the numbers less ` ` ` `// than the 4 ` ` ` `for` `(` `int` `j = 1; j < n + 1; j++) ` ` ` `Console.Write(j + ` `" "` `); ` ` ` ` ` `Console.WriteLine(); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// ints 1, 2 and 3 can be ` ` ` `// grouped into one group ` ` ` `Console.WriteLine(` `"1 2 3"` `); ` ` ` `for` `(` `int` `j = 4; j < n; j += 2) ` ` ` `{ ` ` ` `// Consecutive even and ` ` ` `// odd numbers ` ` ` `Console.WriteLine(j + ` `" "` `+ (j + 1)); ` ` ` ` ` `if` `(n % 2 == 0) ` ` ` `Console.WriteLine(n); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 9; ` ` ` ` ` `// Function Call ` ` ` `mutually_coprime(n); ` `} ` `} ` `// This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

**Output:**

1 2 3 4 5 6 7 8 9

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.

## Recommended Posts:

- Print all Coprime path of a Binary Tree
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Print all distinct Coprime sets possible from 1 to N
- Count all pairs of divisors of a number N whose sum is coprime with N
- Check if all the pairs of an array are coprime with each other
- Finding a Non Transitive Coprime Triplet in a Range
- Largest number less than or equal to N/2 which is coprime to N
- Composite XOR and Coprime AND
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Partition first N natural number into two sets such that their sum is not coprime
- Largest Coprime Set Between two integers
- Count of integers up to N which are non divisors and non coprime with N
- Coprime divisors of a number
- Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)
- Find sum of N-th group of Natural Numbers
- SQL | GROUP BY
- Number Theory | Generators of finite cyclic group under addition
- Ways of dividing a group into two halves such that two elements are in different groups
- Ways of selecting men and women from a group to make a team
- Minimize the sum of the squares of the sum of elements of each group the array is divided into

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.