text: Split squares of first N natural numbers into two sets with minimum absolute difference of their sums | C ++ program for the above approach ; 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
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumSubsetDifference ( int N ) { int blockOfSize8 = N / 8 ; string str = " ABBABAAB " ; int subsetDifference = 0 ; string partition = " " ; while ( blockOfSize8 -- ) { partition += str ; } vector < int > A , B ; for ( int i = 0 ; i < N ; i ++ ) { if ( partition [ i ] == ' A ' ) { A . push_back ( ( i + 1 ) * ( i + 1 ) ) ; } else { B . push_back ( ( i + 1 ) * ( i + 1 ) ) ; } } cout << subsetDifference << " STRNEWLINE " ; for ( int i = 0 ; i < A . size ( ) ; i ++ ) cout << A [ i ] << " ▁ " ; cout << " STRNEWLINE " ; for ( int i = 0 ; i < B . size ( ) ; i ++ ) cout << B [ i ] << " ▁ " ; } int main ( ) { int N = 8 ; minimumSubsetDifference ( N ) ; return 0 ; }