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 = 5Output:10 5

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

Input:N = 2Output:-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;` `}` |

## 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.` |

## 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` |

## 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...` |

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