Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
476 B
1
Indexable
Never

// Entry function
vector<int> subsetSums(vector<int> arr, int N)
{
    vector<int> res;
    subsetSumsUtil(arr, N, res, 0, 0);
    return res;
}

void subsetSumsUtil(vector<int> &arr, int N, vector<int> &res, int i, int sum) {
    if (i == N) {
        res.push_back(sum);
        return;
    }
    
    // Not pick the element at index i
    subsetSumsUtil(arr, N, res, i+1, sum);
    
    // Pick the element at index i
    subsetSumsUtil(arr, N, res, i+1, sum + arr[i]);
}