Divide 1 to n into two groups with minimum sum difference | C # program to divide n integers in two groups such that absolute difference of their sum is minimum ; 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 group1 otherwise include in group2 ; Decrease sum of group1 ; Print both the groups ; Driver code
using System ; using System . Collections ; class GFG { static void printVector ( ArrayList v ) { Console . WriteLine ( v . Count ) ; for ( int i = 0 ; i < v . Count ; i ++ ) Console . Write ( v [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } static void findTwoGroup ( int n ) { int sum = n * ( n + 1 ) / 2 ; int group1Sum = sum / 2 ; ArrayList group1 = new ArrayList ( ) ; ArrayList group2 = new ArrayList ( ) ; for ( int i = n ; i > 0 ; i -- ) { if ( group1Sum - i >= 0 ) { group1 . Add ( i ) ; group1Sum -= i ; } else { group2 . Add ( i ) ; } } printVector ( group1 ) ; printVector ( group2 ) ; } public static void Main ( ) { int n = 5 ; findTwoGroup ( n ) ; } }