Created
June 25, 2013 21:53
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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