Minimize the absolute difference of sum of two subsets | Java program for Minimize the absolute difference of sum of two subsets ; function to print difference ; summation of n elements ; if divisible by 4 ; 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. ; 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 ) . ; Driver program to test above function
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 ) ; } }