Divide first N natural numbers into 3 equal sum subsets | C ++ implementation of the approach ; Function that returns true if the subsets are possible ; If n <= 3 then it is not possible to divide the elements in three subsets satisfying the given conditions ; Sum of all the elements in the range [ 1 , n ] ; If the sum is divisible by 3 then it is possible ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool possible ( int n ) { if ( n > 3 ) { int sum = ( n * ( n + 1 ) ) / 2 ; if ( sum % 3 == 0 ) { return true ; } } return false ; } int main ( ) { int n = 5 ; if ( possible ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }