Skip to content

Instantly share code, notes, and snippets.

@ygabo
Created June 25, 2013 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ygabo/5862808 to your computer and use it in GitHub Desktop.
Save ygabo/5862808 to your computer and use it in GitHub Desktop.
How many subsets in the current set have all the elements but one sum up to the last element.
#include <iostream>
#include <vector>
using namespace std;
int xcount = 0;
void getset( int i, int j, vector<int> nums, int sum){
if( j >= i ) return ;
if( sum+nums[j] == nums[i] ){
xcount++;
return;
}
if( sum + nums[j] < nums[i] )
getset( i, j+1, nums, sum+nums[j]);
if( sum < nums[i] )
getset( i, j+1, nums, sum);
return;
}
int main(){
// hardcoded from the question, sorry
int x[] = {3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99};
//int x[] = {1, 2, 3,4,5 };//, 4, 6};
vector<int> nums(begin(x), end(x));
int max = 1;
int current = 0;
// start at two, minimum number to get x+x=y
for( int i = 2; i < nums.size(); ++i){
getset(i,0,nums,0);
}
cout << xcount << endl;
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment