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)

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:

- Split N natural numbers into two sets having GCD of their sums greater than 1
- Print all distinct Coprime sets possible from 1 to N
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Check if product of first N natural numbers is divisible by their sum
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Divide first N natural numbers into 3 equal sum subsets
- Partition an array such into maximum increasing segments
- Largest Coprime Set Between two integers
- Sum of first N natural numbers which are not powers of K
- Find the permutation of first N natural numbers such that sum of i % P
_{i}is maximum possible - Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Largest number less than or equal to N/2 which is coprime to N
- Coprime divisors of a number
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Sum of sum-series of first N Natural numbers

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.