Split N natural numbers into two sets having GCD of their sums greater than 1
Last Updated :
24 Jul, 2023
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 = 5
Output:
2 4
1 3 5
Explanation:
GCD(Sum({2, 4}), Sum({1, 3, 5}) = GCD(6, 9) = 3
Input: N = 2
Output: -1
Explanation:
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++
#include <bits/stdc++.h>
using namespace std;
void createSets( int N)
{
if (N <= 2) {
cout << "-1" << endl;
return ;
}
for ( int i = 2; i <= N; i += 2)
cout << i << " " ;
cout << "\n" ;
for ( int i = 1; i <= N; i += 2) {
cout << i << " " ;
}
}
int main()
{
int N = 6;
createSets(N);
return 0;
}
|
Java
class GFG{
static void createSets( int N)
{
if (N <= 2 )
{
System.out.println( "-1" );
return ;
}
for ( int i = 2 ; i <= N; i += 2 )
System.out.print(i + " " );
System.out.print( "\n" ) ;
for ( int i = 1 ; i <= N; i += 2 )
{
System.out.print(i + " " );
}
}
public static void main(String[] args)
{
int N = 6 ;
createSets(N);
}
}
|
Python3
def createSets(N):
if (N < = 2 ):
print ( "-1" );
return ;
for i in range ( 2 , N + 1 , 2 ):
print (i, end = " " );
print ("");
for i in range ( 1 , N + 1 , 2 ):
print (i, end = " " );
if __name__ = = '__main__' :
N = 6 ;
createSets(N);
|
C#
using System;
class GFG{
static void createSets( int N)
{
if (N <= 2)
{
Console.WriteLine( "-1" );
return ;
}
for ( int i = 2; i <= N; i += 2)
Console.Write(i + " " );
Console.Write( "\n" ) ;
for ( int i = 1; i <= N; i += 2)
{
Console.Write(i + " " );
}
}
public static void Main(String[] args)
{
int N = 6;
createSets(N);
}
}
|
Javascript
<script>
function createSets(N)
{
if (N <= 2) {
document.write( "-1" );
return ;
}
for (let i = 2; i <= N; i += 2)
document.write(i + " " );
document.write( "</br>" );
for (let i = 1; i <= N; i += 2) {
document.write(i + " " );
}
}
let N = 6;
createSets(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1),since no extra space has been taken.
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.
- 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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void createSets(ll n)
{
if (n <= 2) {
cout << "-1" ;
return ;
}
else {
ll x = (n % 2 == 0) ? (n / 2)
: ((n + 1) / 2);
cout << x << endl;
for (ll i = 1; i <= n; i++) {
if (i == x)
continue ;
cout << i << " " ;
}
}
return ;
}
int main()
{
ll N = 7;
createSets(N);
return 0;
}
|
Java
class GFG{
static void createSets( long n)
{
if (n <= 2 )
{
System.out.print( "-1" );
return ;
}
else
{
long x = (n % 2 == 0 ) ? (n / 2 ) :
((n + 1 ) / 2 );
System.out.print(x + "\n" );
for ( int i = 1 ; i <= n; i++)
{
if (i == x)
continue ;
System.out.print(i + " " );
}
}
return ;
}
public static void main(String[] args)
{
long N = 7 ;
createSets(N);
}
}
|
Python3
def createSets(n):
if (n < = 2 ):
print ( "-1" );
return ;
else :
x = (n / / 2 ) if (n % 2 = = 0 ) else (
(n + 1 ) / / 2 );
print (x, " " );
for i in range ( 1 , n + 1 ):
if (i = = x):
continue ;
print (i, end = " " );
return ;
if __name__ = = '__main__' :
N = 7 ;
createSets(N);
|
C#
using System;
class GFG{
static void createSets( long n)
{
if (n <= 2)
{
Console.Write( "-1" );
return ;
}
else
{
long x = (n % 2 == 0) ? (n / 2) :
((n + 1) / 2);
Console.Write(x + "\n" );
for ( int i = 1; i <= n; i++)
{
if (i == x)
continue ;
Console.Write(i + " " );
}
}
return ;
}
public static void Main(String[] args)
{
long N = 7;
createSets(N);
}
}
|
Javascript
<script>
function createSets(n)
{
if (n <= 2) {
document.write( "-1" );
return ;
}
else {
let x = (n % 2 == 0) ? (n / 2) : ((n + 1) / 2);
document.write(x + "</br>" );
for (let i = 1; i <= n; i++)
{
if (i == x)
continue ;
document.write(i + " " );
}
}
return ;
}
let N = 7;
createSets(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...