Given an integer **N**, the task is to create two sets of distinct elements from **1 to N **such that the gcd of their respective sums is greater than 1. Print the respective sets. If no such split can be done, print -1.**Examples:**

Input:N = 5Output:

2 4

1 3 5Explanation:

GCD(Sum({2, 4}), Sum({1, 3, 5}) = GCD(6, 9) = 3

Input:N = 2Output:-1Explanation:

For N = 2, it is not possible to divide into two sets having GCD of their sum greater than 1.

**Approach 1:** A simple approach is to split all even numbers up to N into one set and all odd numbers into another set.

Below code is the implementation of the approach:

## C++

`// C++ program to split N natural numbers ` `// into two sets having GCD ` `// of their sums greater than 1 ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to create ` `// and print the two sets ` `void` `createSets(` `int` `N) ` `{ ` ` ` `// No such split ` ` ` `// possible for N <= 2 ` ` ` `if` `(N <= 2) { ` ` ` `cout << ` `"-1"` `<< endl; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print the first set ` ` ` `// consisting of even elements ` ` ` `for` `(` `int` `i = 2; i <= N; i += 2) ` ` ` `cout << i << ` `" "` `; ` ` ` `cout << ` `"\n"` `; ` ` ` `// Print the second set ` ` ` `// consisting of odd ones ` ` ` `for` `(` `int` `i = 1; i <= N; i += 2) { ` ` ` `cout << i << ` `" "` `; ` ` ` `} ` `} ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 6; ` ` ` `createSets(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to split N natural numbers ` `// into two sets having GCD ` `// of their sums greater than 1 ` `class` `GFG{ ` ` ` `// Function to create ` `// and print the two sets ` `static` `void` `createSets(` `int` `N) ` `{ ` ` ` `// No such split ` ` ` `// possible for N <= 2 ` ` ` `if` `(N <= ` `2` `) ` ` ` `{ ` ` ` `System.out.println(` `"-1"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print the first set ` ` ` `// consisting of even elements ` ` ` `for` `(` `int` `i = ` `2` `; i <= N; i += ` `2` `) ` ` ` `System.out.print(i + ` `" "` `); ` ` ` `System.out.print(` `"\n"` `) ; ` ` ` ` ` `// Print the second set ` ` ` `// consisting of odd ones ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i += ` `2` `) ` ` ` `{ ` ` ` `System.out.print(i + ` `" "` `); ` ` ` `} ` `} ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `int` `N = ` `6` `; ` ` ` `createSets(N); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to split N natural numbers ` `# into two sets having GCD ` `# of their sums greater than 1 ` ` ` `# Function to create ` `# and prthe two sets ` `def` `createSets(N): ` ` ` ` ` `# No such split ` ` ` `# possible for N <= 2 ` ` ` `if` `(N <` `=` `2` `): ` ` ` `print` `(` `"-1"` `); ` ` ` `return` `; ` ` ` ` ` `# Prthe first set ` ` ` `# consisting of even elements ` ` ` `for` `i ` `in` `range` `(` `2` `, N ` `+` `1` `, ` `2` `): ` ` ` `print` `(i, end` `=` `" "` `); ` ` ` `print` `(""); ` ` ` ` ` `# Prthe second set ` ` ` `# consisting of odd ones ` ` ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `, ` `2` `): ` ` ` `print` `(i, end ` `=` `" "` `); ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `6` `; ` ` ` `createSets(N); ` ` ` `# This code is contributed by gauravrajput1` |

*chevron_right*

*filter_none*

## C#

`// C# program to split N natural numbers ` `// into two sets having GCD ` `// of their sums greater than 1 ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to create ` `// and print the two sets ` `static` `void` `createSets(` `int` `N) ` `{ ` ` ` `// No such split ` ` ` `// possible for N <= 2 ` ` ` `if` `(N <= 2) ` ` ` `{ ` ` ` `Console.WriteLine(` `"-1"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print the first set ` ` ` `// consisting of even elements ` ` ` `for` `(` `int` `i = 2; i <= N; i += 2) ` ` ` `Console.Write(i + ` `" "` `); ` ` ` `Console.Write(` `"\n"` `) ; ` ` ` ` ` `// Print the second set ` ` ` `// consisting of odd ones ` ` ` `for` `(` `int` `i = 1; i <= N; i += 2) ` ` ` `{ ` ` ` `Console.Write(i + ` `" "` `); ` ` ` `} ` `} ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `int` `N = 6; ` ` ` `createSets(N); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110` |

*chevron_right*

*filter_none*

**Output:**

2 4 6 1 3 5

**Approach 2:** This approach can be divided into 2 cases based on N:

**If N is odd:**- The sum of N natural numbers is divisible by
**(N+1)/2**in this case. - Therefore we only need to put
**(N+1)/2 into first set**and the remaining numbers into second set. - This will automatically make the GCD of their sums greater than 1.

- The sum of N natural numbers is divisible by
**If N is even:**- The sum of N natural numbers is divisible by
**N/2**in this case. - Therefore we only need to put
**N/2 into first set**and the remaining numbers into second set. - This will automatically make the GCD of their sums greater than 1.

- The sum of N natural numbers is divisible by

Below is the implementation of the above approach:

## C++

`// C++ program to split N natural numbers ` `// into two sets having GCD ` `// of their sums greater than 1 ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define ll long long ` ` ` `// Function to create ` `// and print the two sets ` `void` `createSets(ll n) ` `{ ` ` ` `// For n <= 2 such sets ` ` ` `// can never be formed ` ` ` `if` `(n <= 2) { ` ` ` `cout << ` `"-1"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `else` `{ ` ` ` ` ` `// Check if N is even or odd ` ` ` `// and store the element ` ` ` `// which divides the sum of N ` ` ` `// natural numbers accordingly ` ` ` `ll x = (n % 2 == 0) ? (n / 2) ` ` ` `: ((n + 1) / 2); ` ` ` ` ` `// First set ` ` ` `cout << x << endl; ` ` ` ` ` `// Print elements of second set ` ` ` `for` `(ll i = 1; i <= n; i++) { ` ` ` ` ` `if` `(i == x) ` ` ` `continue` `; ` ` ` ` ` `cout << i << ` `" "` `; ` ` ` `} ` ` ` `} ` ` ` `return` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ll N = 7; ` ` ` ` ` `createSets(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to split N natural numbers ` `// into two sets having GCD ` `// of their sums greater than 1 ` `class` `GFG{ ` ` ` `// Function to create ` `// and print the two sets ` `static` `void` `createSets(` `long` `n) ` `{ ` ` ` ` ` `// For n <= 2 such sets ` ` ` `// can never be formed ` ` ` `if` `(n <= ` `2` `) ` ` ` `{ ` ` ` `System.out.print(` `"-1"` `); ` ` ` `return` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Check if N is even or odd ` ` ` `// and store the element ` ` ` `// which divides the sum of N ` ` ` `// natural numbers accordingly ` ` ` `long` `x = (n % ` `2` `== ` `0` `) ? (n / ` `2` `) : ` ` ` `((n + ` `1` `) / ` `2` `); ` ` ` ` ` `// First set ` ` ` `System.out.print(x + ` `"\n"` `); ` ` ` ` ` `// Print elements of second set ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `{ ` ` ` `if` `(i == x) ` ` ` `continue` `; ` ` ` ` ` `System.out.print(i + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` `return` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `long` `N = ` `7` `; ` ` ` ` ` `createSets(N); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to split N natural numbers ` `# into two sets having GCD ` `# of their sums greater than 1 ` ` ` `# Function to create ` `# and prthe two sets ` `def` `createSets(n): ` ` ` ` ` `# For n <= 2 such sets ` ` ` `# can never be formed ` ` ` `if` `(n <` `=` `2` `): ` ` ` `print` `(` `"-1"` `); ` ` ` `return` `; ` ` ` `else` `: ` ` ` ` ` `# Check if N is even or odd ` ` ` `# and store the element ` ` ` `# which divides the sum of N ` ` ` `# natural numbers accordingly ` ` ` `x ` `=` `(n ` `/` `/` `2` `) ` `if` `(n ` `%` `2` `=` `=` `0` `) ` `else` `( ` ` ` `(n ` `+` `1` `) ` `/` `/` `2` `); ` ` ` ` ` `# First set ` ` ` `print` `(x, ` `" "` `); ` ` ` ` ` `# Prelements of second set ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` `if` `(i ` `=` `=` `x): ` ` ` `continue` `; ` ` ` ` ` `print` `(i, end ` `=` `" "` `); ` ` ` ` ` `return` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `7` `; ` ` ` ` ` `createSets(N); ` ` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to split N natural numbers ` `// into two sets having GCD ` `// of their sums greater than 1 ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to create ` `// and print the two sets ` `static` `void` `createSets(` `long` `n) ` `{ ` ` ` ` ` `// For n <= 2 such sets ` ` ` `// can never be formed ` ` ` `if` `(n <= 2) ` ` ` `{ ` ` ` `Console.Write(` `"-1"` `); ` ` ` `return` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Check if N is even or odd ` ` ` `// and store the element ` ` ` `// which divides the sum of N ` ` ` `// natural numbers accordingly ` ` ` `long` `x = (n % 2 == 0) ? (n / 2) : ` ` ` `((n + 1) / 2); ` ` ` ` ` `// First set ` ` ` `Console.Write(x + ` `"\n"` `); ` ` ` ` ` `// Print elements of second set ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` `if` `(i == x) ` ` ` `continue` `; ` ` ` ` ` `Console.Write(i + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` `return` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `long` `N = 7; ` ` ` ` ` `createSets(N); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

**Output:**

4 1 2 3 5 6 7

**Time Complexity:** *O(N)* **Auxiliary Space Complexity:*** O(1)*

## Recommended Posts:

- First N natural can be divided into two sets with given difference and co-prime sums
- Possible two sets from first N natural numbers difference of sums as D
- Partition first N natural number into two sets such that their sum is not coprime
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Largest subsequence having GCD greater than 1
- Split Array into min number of subsets with difference between each pair greater than 1
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Split array into two subarrays such that difference of their sum is minimum
- Split an array containing N elements into K sets of distinct elements
- First element of every K sets having consecutive elements with exactly K prime factors less than N
- Check if an array can be split into subarrays with GCD exceeding K
- Sum of square-sums of first n natural numbers
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Split array into K subsets to maximize their sum of maximums and minimums
- GCD of array is greater than one
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Count ways to split a Binary String into three substrings having equal count of zeros
- Split numbers from 1 to N into two equal sum subsets

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.