Divide 1 to n into two groups with minimum sum difference | To print vector along size ; Print vector size ; Print vector elements ; To divide n in two groups such that absolute difference of their sum is minimum ; Find sum of all elements upto n ; Sum of elements of group1 ; If sum is greater then or equal to 0 include i in group 1 otherwise include in group2 ; Decrease sum of group1 ; Print both the groups ; Driver program to test above functions
function printVector ( v ) { document . write ( v . length + " " ) ; for ( var i = 0 ; i < v . length ; i ++ ) document . write ( v [ i ] + " " ) ; document . write ( " " ) ; } function findTwoGroup ( n ) { var sum = n * ( n + 1 ) / 2 ; var group1Sum = parseInt ( sum / 2 ) ; var group1 = [ ] , group2 = [ ] ; for ( var i = n ; i > 0 ; i -- ) { if ( group1Sum - i >= 0 ) { group1 . push ( i ) ; group1Sum -= i ; } else { group2 . push ( i ) ; } } printVector ( group1 ) ; printVector ( group2 ) ; } var n = 5 ; findTwoGroup ( n ) ;