Split N powers of 2 into two subsets such that their difference of sum is minimum | javascript program to find the minimum difference possible by splitting all powers of 2 up to N into two sets of equal size ; Store the largest ; Form two separate groups ; Initialize the sum for two groups as 0 ; Insert 2 ^ n in the first group ; Calculate the sum of least n / 2 - 1 elements added to the first set ; Sum of remaining n / 2 - 1 elements ; Min difference between the two groups ; Driver Code
function MinDiff ( n ) { var val = parseInt ( Math . pow ( 2 , n ) ) ; var sep = n / 2 ; var grp1 = 0 ; var grp2 = 0 ; grp1 = grp1 + val ; for ( i = 1 ; i < sep ; i ++ ) grp1 = grp1 + parseInt ( Math . pow ( 2 , i ) ) ; for ( i = sep ; i < n ; i ++ ) grp2 = grp2 + parseInt ( Math . pow ( 2 , i ) ) ; document . write ( Math . abs ( grp1 - grp2 ) ) ; } var n = 4 ; MinDiff ( n ) ;