Split squares of first N natural numbers into two sets with minimum absolute difference of their sums | Java 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
import java . util . Arrays ; class GFG { static void minimumSubsetDifference ( int N ) { int blockOfSize8 = N / 8 ; String str = " ABBABAAB " ; int subsetDifference = 0 ; String partition = " " ; while ( blockOfSize8 -- > 0 ) { partition += str ; } int A [ ] = new int [ N ] ; int B [ ] = new int [ N ] ; int x = 0 , y = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( partition . charAt ( i ) == ' A ' ) { A [ x ++ ] = ( ( i + 1 ) * ( i + 1 ) ) ; } else { B [ y ++ ] = ( ( i + 1 ) * ( i + 1 ) ) ; } } System . out . println ( subsetDifference ) ; for ( int i = 0 ; i < x ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; System . out . println ( ) ; for ( int i = 0 ; i < y ; i ++ ) System . out . print ( B [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 8 ; minimumSubsetDifference ( N ) ; } }