Perfect Sum Problem | C ++ implementation of the above approach ; Function to print the subsets whose sum is equal to the given target K ; Create the new array with size equal to array set [ ] to create binary array as per n ( decimal number ) ; Convert the array into binary array ; Calculate the sum of this subset ; Check whether sum is equal to target if it is equal , then print the subset ; Function to find the subsets with sum K ; Calculate the total no . of subsets ; Run loop till total no . of subsets and call the function for each subset ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumSubsets ( vector < int > set , int n , int target ) { int x [ set . size ( ) ] ; int j = set . size ( ) - 1 ; while ( n > 0 ) { x [ j ] = n % 2 ; n = n / 2 ; j -- ; } int sum = 0 ; for ( int i = 0 ; i < set . size ( ) ; i ++ ) if ( x [ i ] == 1 ) sum = sum + set [ i ] ; if ( sum == target ) { cout << ( " { " ) ; for ( int i = 0 ; i < set . size ( ) ; i ++ ) if ( x [ i ] == 1 ) cout << set [ i ] << " , ▁ " ; cout << ( " } , ▁ " ) ; } } void findSubsets ( vector < int > arr , int K ) { int x = pow ( 2 , arr . size ( ) ) ; for ( int i = 1 ; i < x ; i ++ ) sumSubsets ( arr , i , K ) ; } int main ( ) { vector < int > arr = { 5 , 10 , 12 , 13 , 15 , 18 } ; int K = 30 ; findSubsets ( arr , K ) ; return 0 ; }