Given a number n, divide first n natural numbers (1, 2, …n) into two subsets such that difference between sums of two subsets is minimum.
Examples:
Input : n = 4
Output : First subset sum = 5,
Second subset sum = 5.
Difference = 0
Explanation:
Subset 1: 1 4
Subset 2: 2 3
Input : n = 6
Output: First subset sum = 10,
Second subset sum = 11.
Difference = 1
Explanation :
Subset 1: 1 3 6
Subset 2: 2 4 5
Approach:
The approach is based on the fact that any four consecutive numbers can be divided into two groups by putting middle two elements in one group and corner elements in other group. So, if n is a multiple of 4 then their difference will be 0, hence the summation of one set will be half of the summation of N elements which can be calculated by using sum = n*(n+1)/2
There are three other cases to consider in which we cannot divide into groups of 4, which will leave a remainder of 1, 2 and 3:
a) If it leaves a remainder of 1, then all other n-1 elements are clubbed into group of 4 hence their sum will be int(sum/2) and the other half sum will be int(sum/2+1) and their difference will always be 1.
b) Above mentioned steps will be followed in case of n%4 == 2 also. Here we form groups of size 4 for elements from 3 onward. Remaining elements would be 1 and 2. 1 goes in one group and 2 goes in other group.
c) When n%4 == 3, then club n-3 elements into groups of 4. The left out elements will be 1, 2 and 3, in which 1 and 2 will go to one set and 3 to the other set which eventually makes the difference to be 0 and summation of each set to be sum/2.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
void subsetDifference( int n)
{
int s = n * (n + 1) / 2;
if (n % 4 == 0) {
cout << "First subset sum = "
<< s / 2;
cout << "\nSecond subset sum = "
<< s / 2;
cout << "\nDifference = " << 0;
}
else {
if (n % 4 == 1 || n % 4 == 2) {
cout << "First subset sum = "
<< s / 2;
cout << "\nSecond subset sum = "
<< s / 2 + 1;
cout << "\nDifference = " << 1;
}
else
{
cout << "First subset sum = "
<< s / 2;
cout << "\nSecond subset sum = "
<< s / 2;
cout << "\nDifference = " << 0;
}
}
}
int main()
{
int n = 6;
subsetDifference(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void subsetDifference( int n)
{
int s = n * (n + 1 ) / 2 ;
if (n % 4 == 0 ) {
System.out.println( "First subset sum = " + s / 2 );
System.out.println( "Second subset sum = " + s / 2 );
System.out.println( "Difference = " + 0 );
}
else {
if (n % 4 == 1 || n % 4 == 2 ) {
System.out.println( "First subset sum = " + s / 2 );
System.out.println( "Second subset sum = " + ((s / 2 ) + 1 ));
System.out.println( "Difference = " + 1 );
}
else
{
System.out.println( "First subset sum = " + s / 2 );
System.out.println( "Second subset sum = " + s / 2 );
System.out.println( "Difference = " + 0 );
}
}
}
public static void main(String[] args)
{
int n = 6 ;
subsetDifference(n);
}
}
|
Python3
def subsetDifference( n ):
s = int (n * (n + 1 ) / 2 )
if n % 4 = = 0 :
print ( "First subset sum = " , int (s / 2 ))
print ( "Second subset sum = " , int (s / 2 ))
print ( "Difference = " , 0 )
else :
if n % 4 = = 1 or n % 4 = = 2 :
print ( "First subset sum = " , int (s / 2 ))
print ( "Second subset sum = " , int (s / 2 ) + 1 )
print ( "Difference = " , 1 )
else :
print ( "First subset sum = " , int (s / 2 ))
print ( "Second subset sum = " , int (s / 2 ))
print ( "Difference = " , 0 )
n = 6
subsetDifference(n)
|
C#
using System;
class GFG {
static void subsetDifference( int n)
{
int s = n * (n + 1) / 2;
if (n % 4 == 0) {
Console.WriteLine( "First "
+ "subset sum = " + s / 2);
Console.WriteLine( "Second "
+ "subset sum = " + s / 2);
Console.WriteLine( "Difference"
+ " = " + 0);
}
else {
if (n % 4 == 1 || n % 4 == 2) {
Console.WriteLine( "First "
+ "subset sum = " + s / 2);
Console.WriteLine( "Second "
+ "subset sum = " + ((s / 2)
+ 1));
Console.WriteLine( "Difference"
+ " = " + 1);
}
else
{
Console.WriteLine( "First "
+ "subset sum = " + s / 2);
Console.WriteLine( "Second "
+ "subset sum = " + s / 2);
Console.WriteLine( "Difference"
+ " = " + 0);
}
}
}
public static void Main()
{
int n = 6;
subsetDifference(n);
}
}
|
PHP
<?php
function subsetDifference( $n )
{
$s = $n * ( $n + 1) / 2;
if ( $n % 4 == 0)
{
echo "First subset sum = "
, floor ( $s / 2);
echo "\nSecond subset sum = "
, floor ( $s / 2);
echo "\nDifference = " , 0;
}
else
{
if ( $n % 4 == 1 || $n % 4 == 2)
{
echo "First subset sum = "
, floor ( $s / 2);
echo "\nSecond subset sum = "
, floor ( $s / 2 + 1);
echo "\nDifference = " ,1;
}
else
{
echo "First subset sum = "
, floor ( $s / 2);
echo "\nSecond subset sum = "
, floor ( $s / 2);
echo "\nDifference = " , 0;
}
}
}
$n = 6;
subsetDifference( $n );
?>
|
Javascript
<script>
function subsetDifference(n)
{
let s = n * (n + 1) / 2;
if (n % 4 == 0)
{
document.write( "First subset sum = " + Math.floor(s / 2));
document.write( "<br> Second subset sum = " + Math.floor(s / 2));
document.write( "<br> Difference = " + 0);
}
else
{
if (n % 4 == 1 || n % 4 == 2)
{
document.write( "First subset sum = " + Math.floor(s / 2));
document.write( "<br> Second subset sum = " + Math.floor(s / 2 + 1));
document.write( "<br> Difference = " + 1);
}
else
{
document.write( "First subset sum = " + Math.floor(s / 2));
document.write( "<br> Second subset sum = " + Math.floor(s / 2));
document.write( "<br> Difference = " + 0);
}
}
}
let n = 6;
subsetDifference(n);
</script>
|
Output
First subset sum = 10
Second subset sum = 11
Difference = 1
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!