# Partition first N natural number into two sets such that their sum is not coprime

Given an integer **N**, the task is to partition the first **N** natural numbers in two non-empty sets such that the sum of these set is not coprime to each other. If it is possible then find the possible partition then print **-1** else print the sum of elements of both the sets.

**Examples:**

Input:N = 5

Output:10 5

{1, 2, 3, 4} and {5} are the valid partitions.

Input:N = 2

Output:-1

**Approach:**

- If
**N ≤ 2**then print**-1**as the only possible partition is**{1}**and**{2}**where sum of both the sets are coprime to each other. - Now if
**N is odd**then we put**N**in one set and first**(N – 1)**numbers into other set. So sum of the two sets will be**N**and**N * (N – 1) / 2**and as their gcd is**N**, they will not be coprime to each other. - If
**N is even**then we do the same thing as previous and sum of the two sets will be**N**and**N * (N – 1) / 2**. As**N**is even,**(N – 1)**is not divisible by 2 but**N**is divisible which gives sum as**(N / 2) * (N – 1)**and their gcd will be**N / 2**. Since**N / 2**is a factor of**N**, so they are no coprime to each other.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the required sets ` `void` `find_set(` `int` `n) ` `{ ` ` ` ` ` `// Impossible case ` ` ` `if` `(n <= 2) { ` ` ` `cout << ` `"-1"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Sum of first n-1 natural numbers ` ` ` `int` `sum1 = (n * (n - 1)) / 2; ` ` ` `int` `sum2 = n; ` ` ` `cout << sum1 << ` `" "` `<< sum2; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 8; ` ` ` `find_set(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the required sets ` `static` `void` `find_set(` `int` `n) ` `{ ` ` ` ` ` `// Impossible case ` ` ` `if` `(n <= ` `2` `) ` ` ` `{ ` ` ` `System.out.println (` `"-1"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Sum of first n-1 natural numbers ` ` ` `int` `sum1 = (n * (n - ` `1` `)) / ` `2` `; ` ` ` `int` `sum2 = n; ` ` ` `System.out.println (sum1 + ` `" "` `+sum2 ); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` ` ` `int` `n = ` `8` `; ` ` ` `find_set(n); ` `} ` `} ` ` ` `// This code is contributed by jit_t. ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the approach ` ` ` `# Function to find the required sets ` `def` `find_set(n): ` ` ` ` ` `# Impossible case ` ` ` `if` `(n <` `=` `2` `): ` ` ` `print` `(` `"-1"` `); ` ` ` `return` `; ` ` ` ` ` `# Sum of first n-1 natural numbers ` ` ` `sum1 ` `=` `(n ` `*` `(n ` `-` `1` `)) ` `/` `2` `; ` ` ` `sum2 ` `=` `n; ` ` ` `print` `(sum1, ` `" "` `, sum2); ` ` ` `# Driver code ` `n ` `=` `8` `; ` `find_set(n); ` ` ` `# This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the required sets ` `static` `void` `find_set(` `int` `n) ` `{ ` ` ` ` ` `// Impossible case ` ` ` `if` `(n <= 2) ` ` ` `{ ` ` ` `Console.WriteLine(` `"-1"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Sum of first n-1 natural numbers ` ` ` `int` `sum1 = (n * (n - 1)) / 2; ` ` ` `int` `sum2 = n; ` ` ` `Console.WriteLine(sum1 + ` `" "` `+sum2 ); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` ` ` `int` `n = 8; ` ` ` `find_set(n); ` `} ` `} ` ` ` `// This code is contributed by anuj_67... ` |

*chevron_right*

*filter_none*

**Output:**

28 8

**Time Complexity:** O(1)

## Recommended Posts:

- Largest number less than or equal to N/2 which is coprime to N
- Minimum number of sets with numbers less than Y
- Finding a Non Transitive Coprime Triplet in a Range
- Number of compositions of a natural number
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Greatest divisor which divides all natural number in range [L, R]
- Find sum of divisors of all the divisors of a natural number
- Cartesian Product of Two Sets
- Ways to select one or more pairs from two different sets
- Counting sets of 1s and 0s in a binary matrix
- Count sub-sets that satisfy the given condition
- Maximum String Partition
- Count of sub-sets of size n with total element sum divisible by 3
- Partition an array such into maximum increasing segments
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum

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.