Split squares of first N natural numbers into two sets with minimum absolute difference of their sums | Function to partition squares of N natural number in two subset ; Store the count of blocks of size 8 ; Partition of block of 8 element ; Store the minimum subset difference ; Partition of N elements to minimize their subset sum difference ; Store elements of subset A and B ; If element is of type A ; If the element is of type B ; Print the minimum subset difference ; Print the first subset ; Print the second subset ; Driver code ; Function Call
function minimumSubsetDifference ( N ) { let blockOfSize8 = N / 8 ; let str = " " ; let subsetDifference = 0 ; let partition = " " ; while ( blockOfSize8 -- > 0 ) { partition += str ; } let A = [ ] ; let B = [ ] ; let x = 0 , y = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( partition [ i ] == ' ' ) { A [ x ++ ] = ( ( i + 1 ) * ( i + 1 ) ) ; } else { B [ y ++ ] = ( ( i + 1 ) * ( i + 1 ) ) ; } } document . write ( subsetDifference + " " ) ; for ( let i = 0 ; i < x ; i ++ ) document . write ( A [ i ] + " " ) ; document . write ( " " ) ; for ( let i = 0 ; i < y ; i ++ ) document . write ( B [ i ] + " " ) ; } let N = 8 ; minimumSubsetDifference ( N ) ;