Split numbers from 1 to N into two equal sum subsets
Last Updated :
25 Sep, 2023
Given an integer N, the task is to divide the numbers from 1 to N into two nonempty subsets such that the sum of elements in the set is equal. Print the element in the subset. If we can’t form any subset then print -1.
Examples:
Input N = 4
Output:
Size of subset 1 is: 2
Elements of the subset are: 1 4
Size of subset 2 is: 2
Elements of the subset are: 2 3
Explanation:
The first and the second set have equal sum that is 5.
Input: N = 8
Output:
Size of subset 1 is: 4
Elements of the subset are: 1 8 3 6
Size of subset 2 is: 4
Elements of the subset are: 2 7 4 5
Explanation:
The first and the second set have equal sum that is 18.
Approach: To solve the problem mentioned above we have to observe the three cases for integer N. Below are the observations:
- Sum of First N natural numbers is odd: Solution is not possible and the answer will be -1. Because we can’t split the odd sum into 2 equal halfs.
- Sum of First N natural numbers is even: just loop from last element to first element and take the elements in the set-1 whose value is less than the half of sum , Maintain another set-2 to store the ans which are not included in set-1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void display(vector< int > &v) {
for ( auto i : v) {
cout << i << " " ;
}
cout << endl;
}
void FindAns( int n) {
int sum = n * (n + 1) / 2;
vector< int > a, b;
if (sum % 2 == 1) {
cout << -1 << endl;
return ;
}
else {
int ans = sum / 2;
for ( int i = n; i >= 1; i--) {
if (i <= ans) {
a.push_back(i);
ans -= i;
}
else {
b.push_back(i);
}
}
cout << "Size of subset 1 is : " ;
cout << a.size() << endl;
cout << "Elements of subset are : " ;
display(a);
cout << "Size of subset 2 is : " ;
cout << b.size() << endl;
cout << "Elements of subset are : " ;
display(b);
}
}
int main() {
int n = 8;
FindAns(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void findAns( int N)
{
if (N <= 2 )
{
System.out.print( "-1" );
return ;
}
int value = (N * (N + 1 )) / 2 ;
if (value& 1 )
{
System.out.print( "-1" );
return ;
}
Vector<Integer> v1 = new Vector<Integer>();
Vector<Integer> v2 = new Vector<Integer>();
if ((N & 1 ) == 0 )
{
int turn = 1 ;
int start = 1 ;
int last = N;
while (start < last)
{
if (turn == 1 )
{
v1.add(start);
v1.add(last);
turn = 0 ;
}
else
{
v2.add(start);
v2.add(last);
turn = 1 ;
}
start++;
last--;
}
}
else
{
int rem = value / 2 ;
boolean [] vis = new boolean [N + 1 ];
for ( int i = 1 ; i <= N; i++)
vis[i] = false ;
vis[ 0 ] = true ;
for ( int i = N; i >= 1 ; i--)
{
if (rem > i)
{
v1.add(i);
vis[i] = true ;
rem -= i;
}
else
{
v1.add(rem);
vis[rem] = true ;
break ;
}
}
for ( int i = 1 ; i <= N; i++)
{
if (!vis[i])
v2.add(i);
}
}
System.out.print( "Size of subset 1 is: " );
System.out.println(v1.size());
System.out.print( "Elements of the subset are: " );
for (Integer c : v1)
System.out.print(c + " " );
System.out.println();
System.out.print( "Size of subset 2 is: " );
System.out.println(v2.size());
System.out.print( "Elements of the subset are: " );
for (Integer c : v2)
System.out.print(c + " " );
}
public static void main(String[] args)
{
int N = 8 ;
findAns(N);
}
}
|
Python3
def findAns(N):
if (N < = 2 ):
print ( "-1" )
return
value = (N * (N + 1 )) / / 2
if (value & 1 ):
print ( "-1" )
return
v1 = []
v2 = []
if ( not (N & 1 )):
turn = 1
start = 1
last = N
while (start < last):
if (turn):
v1.append(start)
v1.append(last)
turn = 0
else :
v2.append(start)
v2.append(last)
turn = 1
start + = 1
last - = 1
else :
rem = value / / 2
vis = [ False ] * (N + 1 )
for i in range ( 1 , N + 1 ):
vis[i] = False
vis[ 0 ] = True
for i in range (N , 0 , - 1 ):
if (rem > i):
v1.append(i)
vis[i] = True
rem - = i
else :
v1.append(rem)
vis[rem] = True
break
for i in range ( 1 , N + 1 ):
if ( not vis[i]):
v2.append(i)
print ( "Size of subset 1 is: " ,
end = "")
print ( len ( v1))
print ( "Elements of the subset are: " ,
end = "")
for c in v1:
print (c, end = " " )
print ()
print ( "Size of subset 2 is: " ,
end = "")
print ( len ( v2))
print ( "Elements of the subset are: " ,
end = "")
for c in v2:
print (c, end = " " )
if __name__ = = "__main__" :
N = 8
findAns(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void findAns( int N)
{
if (N <= 2)
{
Console.Write( "-1" );
return ;
}
int value = (N * (N + 1)) / 2;
if (value&1)
{
Console.Write( "-1" );
return ;
}
List< int > v1 = new List< int >();
List< int > v2 = new List< int >();
if ((N & 1) == 0)
{
int turn = 1;
int start = 1;
int last = N;
while (start < last)
{
if (turn == 1)
{
v1.Add(start);
v1.Add(last);
turn = 0;
}
else
{
v2.Add(start);
v2.Add(last);
turn = 1;
}
start++;
last--;
}
}
else
{
int rem = value / 2;
bool [] vis = new bool [N + 1];
for ( int i = 1; i <= N; i++)
vis[i] = false ;
vis[0] = true ;
for ( int i = N; i >= 1; i--)
{
if (rem > i)
{
v1.Add(i);
vis[i] = true ;
rem -= i;
}
else
{
v1.Add(rem);
vis[rem] = true ;
break ;
}
}
for ( int i = 1; i <= N; i++)
{
if (!vis[i])
v2.Add(i);
}
}
Console.Write( "Size of subset 1 is: " );
Console.WriteLine(v1.Count);
Console.Write( "Elements of the subset are: " );
foreach ( int c in v1)
Console.Write(c + " " );
Console.WriteLine();
Console.Write( "Size of subset 2 is: " );
Console.WriteLine(v2.Count);
Console.Write( "Elements of the subset are: " );
foreach ( int c in v2)
Console.Write(c + " " );
}
public static void Main(String[] args)
{
int N = 8;
findAns(N);
}
}
|
Javascript
<script>
function findAns(N)
{
if (N <= 2)
{
document.write( "-1" );
return ;
}
let value = Math.floor((N * (N + 1)) / 2);
if (value&1)
{
document.write( "-1" );
return ;
}
let v1 = [];
let v2 = [];
if ((N & 1) == 0)
{
let turn = 1;
let start = 1;
let last = N;
while (start < last)
{
if (turn == 1)
{
v1.push(start);
v1.push(last);
turn = 0;
}
else
{
v2.push(start);
v2.push(last);
turn = 1;
}
start++;
last--;
}
}
else
{
let rem = Math.floor(value / 2);
let vis = new Array(N + 1);
for (let i = 1; i <= N; i++)
vis[i] = false ;
vis[0] = true ;
for (let i = N; i >= 1; i--)
{
if (rem > i)
{
v1.push(i);
vis[i] = true ;
rem -= i;
}
else
{
v1.push(rem);
vis[rem] = true ;
break ;
}
}
for (let i = 1; i <= N; i++)
{
if (!vis[i])
v2.push(i);
}
}
document.write( "Size of subset 1 is: " );
document.write(v1.length+'<br>');
document.write( "Elements of the subset are: " );
for (let c=0;c<v1.length;c++)
document.write(v1 + " " );
document.write( "<br>" );
document.write( "Size of subset 2 is: " );
document.write(v2.length+ "<br>" );
document.write( "Elements of the subset are: " );
for (let c=0;c< v2.length;c++)
document.write(v2 + " " );
}
let N = 8;
findAns(N);
</script>
|
Output
Size of subset 1 is : 3
Elements of subset are : 8 7 3
Size of subset 2 is : 5
Elements of subset are : 6 5 4 2 1
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...