This function works fine with smaller arrays. But, when, given very large array of "int"s it fails miserably. I looked up and found out its the stack that is out of memory which was causing the problem because it can't allocate enough space to hold all of the inner loop's variables. So how to work around this?
void subsetSums(vector<int> arr, int l, int r, int sum=0) {
if(l > r){
cout << sum << ", ";
return;
}
subsetSums(arr, l+1, r, sum+arr[l]);
subsetSums(arr, l+1, r, sum);
}
int main(){
vector<int> arr(500000, 1);
subsetSums(arr, 0, arr.size()-1);
return 0;
}
I just want to "hot fix" this for now. Then, maybe I will find optimal solution to this problem.