Sum of products of all combination taken ( 1 to n ) at a time | Program to find SOP of all combination taken ( 1 to N ) at a time using brute force ; to store sum of every combination ; if we have reached sufficient depth ; find the product of combination ; add the product into sum ; recursion to produce different combination ; function to print sum of products of all combination taken 1 - N at a time ; creating temporary array for storing combination ; call combination with r = i for combination taken i at a time ; displaying sum ; Driver code ; storing numbers from 1 - N in array ; calling allCombination
import java . io . * ; class GFG { static int sum = 0 ; static void Combination ( int [ ] a , int [ ] combi , int n , int r , int depth , int index ) { if ( index == r ) { int product = 1 ; for ( int i = 0 ; i < r ; i ++ ) product = product * combi [ i ] ; sum += product ; return ; } for ( int i = depth ; i < n ; i ++ ) { combi [ index ] = a [ i ] ; Combination ( a , combi , n , r , i + 1 , index + 1 ) ; } } static void allCombination ( int [ ] a , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int [ ] combi = new int [ i ] ; Combination ( a , combi , n , i , 0 , 0 ) ; System . out . print ( " f ( " + i + " ) ▁ - - > ▁ " + sum + "NEW_LINE"); sum = 0 ; } } public static void main ( String args [ ] ) { int n = 5 ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = i + 1 ; allCombination ( a , n ) ; } }