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

`// CPP program to Minimize the absolute ` `// difference of sum of two subsets ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to print difference ` `void` `subsetDifference(` `int` `n) ` `{ ` ` ` `// summation of n elements ` ` ` `int` `s = n * (n + 1) / 2; ` ` ` ` ` `// if divisible by 4 ` ` ` `if` `(n % 4 == 0) { ` ` ` `cout << ` `"First subset sum = "` ` ` `<< s / 2; ` ` ` `cout << ` `"\nSecond subset sum = "` ` ` `<< s / 2; ` ` ` `cout << ` `"\nDifference = "` `<< 0; ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// if remainder 1 or 2. In case of remainder ` ` ` `// 2, we divide elements from 3 to n in groups ` ` ` `// of size 4 and put 1 in one group and 2 in ` ` ` `// group. This also makes difference 1. ` ` ` `if` `(n % 4 == 1 || n % 4 == 2) { ` ` ` ` ` `cout << ` `"First subset sum = "` ` ` `<< s / 2; ` ` ` `cout << ` `"\nSecond subset sum = "` ` ` `<< s / 2 + 1; ` ` ` `cout << ` `"\nDifference = "` `<< 1; ` ` ` `} ` ` ` ` ` `// We put elements from 4 to n in groups of ` ` ` `// size 4. Remaining elements 1, 2 and 3 can ` ` ` `// be divided as (1, 2) and (3). ` ` ` `else` ` ` `{ ` ` ` `cout << ` `"First subset sum = "` ` ` `<< s / 2; ` ` ` `cout << ` `"\nSecond subset sum = "` ` ` `<< s / 2; ` ` ` `cout << ` `"\nDifference = "` `<< 0; ` ` ` `} ` ` ` `} ` `} ` ` ` `// driver program to test the above function ` `int` `main() ` `{ ` ` ` `int` `n = 6; ` ` ` `subsetDifference(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for Minimize the absolute ` `// difference of sum of two subsets ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// function to print difference ` ` ` `static` `void` `subsetDifference(` `int` `n) ` ` ` `{ ` ` ` `// summation of n elements ` ` ` `int` `s = n * (n + ` `1` `) / ` `2` `; ` ` ` ` ` `// if divisible by 4 ` ` ` `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 remainder 1 or 2. In case of remainder ` ` ` `// 2, we divide elements from 3 to n in groups ` ` ` `// of size 4 and put 1 in one group and 2 in ` ` ` `// group. This also makes difference 1. ` ` ` `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` `); ` ` ` `} ` ` ` ` ` `// We put elements from 4 to n in groups of ` ` ` `// size 4. Remaining elements 1, 2 and 3 can ` ` ` `// be divided as (1, 2) and (3). ` ` ` `else` ` ` `{ ` ` ` `System.out.println(` `"First subset sum = "` `+ s / ` `2` `); ` ` ` `System.out.println(` `"Second subset sum = "` `+ s / ` `2` `); ` ` ` `System.out.println(` `"Difference = "` `+ ` `0` `); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `6` `; ` ` ` `subsetDifference(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Arnav Kr. Mandal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to Minimize the absolute ` `# difference of sum of two subsets ` ` ` `# function to print difference ` `def` `subsetDifference( n ): ` ` ` ` ` `# summation of n elements ` ` ` `s ` `=` `int` `(n ` `*` `(n ` `+` `1` `) ` `/` `2` `) ` ` ` ` ` `# if divisible by 4 ` ` ` `if` `n ` `%` `4` `=` `=` `0` `: ` ` ` `print` `(` `"First subset sum = "` `, ` `int` `(s ` `/` `2` `)) ` ` ` `print` `(` `"Second subset sum = "` `,` `int` `(s ` `/` `2` `)) ` ` ` `print` `(` `"Difference = "` `, ` `0` `) ` ` ` ` ` `else` `: ` ` ` ` ` `# if remainder 1 or 2. In case of remainder ` ` ` `# 2, we divide elements from 3 to n in groups ` ` ` `# of size 4 and put 1 in one group and 2 in ` ` ` `# group. This also makes difference 1. ` ` ` `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` `) ` ` ` ` ` `# We put elements from 4 to n in groups of ` ` ` `# size 4. Remaining elements 1, 2 and 3 can ` ` ` `# be divided as (1, 2) and (3). ` ` ` `else` `: ` ` ` `print` `(` `"First subset sum = "` `, ` `int` `(s ` `/` `2` `)) ` ` ` `print` `(` `"Second subset sum = "` `,` `int` `(s ` `/` `2` `)) ` ` ` `print` `(` `"Difference = "` `, ` `0` `) ` ` ` `# driver code to test the above function ` `n ` `=` `6` `subsetDifference(n) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# program for Minimize the absolute ` `// difference of sum of two subsets ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to print difference ` ` ` `static` `void` `subsetDifference(` `int` `n) ` ` ` `{ ` ` ` ` ` `// summation of n elements ` ` ` `int` `s = n * (n + 1) / 2; ` ` ` ` ` `// if divisible by 4 ` ` ` `if` `(n % 4 == 0) { ` ` ` ` ` `Console.WriteLine(` `"First "` ` ` `+ ` `"subset sum = "` `+ s / 2); ` ` ` ` ` `Console.WriteLine(` `"Second "` ` ` `+ ` `"subset sum = "` `+ s / 2); ` ` ` ` ` `Console.WriteLine(` `"Difference"` ` ` `+ ` `" = "` `+ 0); ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// if remainder 1 or 2. In case ` ` ` `// of remainder 2, we divide ` ` ` `// elements from 3 to n in groups ` ` ` `// of size 4 and put 1 in one ` ` ` `// group and 2 in group. This ` ` ` `// also makes difference 1. ` ` ` `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); ` ` ` `} ` ` ` ` ` `// We put elements from 4 to n ` ` ` `// in groups of size 4. Remaining ` ` ` `// elements 1, 2 and 3 can ` ` ` `// be divided as (1, 2) and (3). ` ` ` `else` ` ` `{ ` ` ` `Console.WriteLine(` `"First "` ` ` `+ ` `"subset sum = "` `+ s / 2); ` ` ` ` ` `Console.WriteLine(` `"Second "` ` ` `+ ` `"subset sum = "` `+ s / 2); ` ` ` ` ` `Console.WriteLine(` `"Difference"` ` ` `+ ` `" = "` `+ 0); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `/* Driver program to test above ` ` ` `function */` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 6; ` ` ` ` ` `subsetDifference(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to Minimize the absolute ` `// difference of sum of two subsets ` ` ` `// function to print difference ` `function` `subsetDifference(` `$n` `) ` `{ ` ` ` ` ` `// summation of n elements ` ` ` `$s` `= ` `$n` `* (` `$n` `+ 1) / 2; ` ` ` ` ` `// if divisible by 4 ` ` ` `if` `(` `$n` `% 4 == 0) ` ` ` `{ ` ` ` `echo` `"First subset sum = "` ` ` `,` `floor` `(` `$s` `/ 2); ` ` ` `echo` `"\nSecond subset sum = "` ` ` `,` `floor` `(` `$s` `/ 2); ` ` ` `echo` `"\nDifference = "` `, 0; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// if remainder 1 or 2. ` ` ` `// In case of remainder ` ` ` `// 2, we divide elements ` ` ` `// from 3 to n in groups ` ` ` `// of size 4 and put 1 in ` ` ` `// one group and 2 in ` ` ` `// group. This also makes ` ` ` `// difference 1. ` ` ` `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; ` ` ` `} ` ` ` ` ` `// We put elements from 4 ` ` ` `// to n in groups of ` ` ` `// size 4. Remaining ` ` ` `// elements 1, 2 and 3 can ` ` ` `// be divided as (1, 2) ` ` ` `// and (3). ` ` ` `else` ` ` `{ ` ` ` `echo` `"First subset sum = "` ` ` `,` `floor` `(` `$s` `/ 2); ` ` ` `echo` `"\nSecond subset sum = "` ` ` `, ` `floor` `(` `$s` `/ 2); ` ` ` `echo` `"\nDifference = "` `, 0; ` ` ` `} ` ` ` `} ` `} ` ` ` ` ` `// Driver code ` ` ` `$n` `= 6; ` ` ` `subsetDifference(` `$n` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

First subset sum = 10 Second subset sum = 11 Difference = 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.

## Recommended Posts:

- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Minimize count of Subsets with difference between maximum and minimum element not exceeding K
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Maximize the difference between two subsets of a set with negatives
- Subsets of size K with product equal to difference of two perfect squares
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Closest sum partition (into two subsets) of numbers from 1 to n
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Split numbers from 1 to N into two equal sum subsets
- Largest subset where absolute difference of any two element is a power of 2
- Partition the array into two odd length groups with minimized absolute difference between their median
- Longest subarray in which absolute difference between any two element is not greater than X
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Maximum distance between two elements whose absolute difference is K

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.