Project Euler #201: Subsets with a unique sum

  • + 0 comments

    My solution ablr to pass only 6 test case how i can optimize this :

    #include<bits/stdc++.h>
    using namespace std;
    map<int,int> mp;
    vector<int> ans;
    void combinationUtil(int arr[], int data[], int start, int end, int index, int r); 
    void printCombination(int arr[], int n, int r) 
    { 
        int data[r];  
        combinationUtil(arr, data, 0, n-1, 0, r); 
    } 
    void combinationUtil(int arr[], int data[], int start, int end, int index, int r) 
    { 
        if (index == r) 
        { 
            int sum=0;
            for (int j=0; j<r; j++) 
                sum+=data[j];
            mp[sum]++;
            ans.push_back(sum);
            return; 
        } 
        for (int i=start; i<=end && end-i+1 >= r-index; i++) 
        { 
            data[index] = arr[i]; 
            combinationUtil(arr, data, i+1, end, index+1, r); 
        } 
    } 
    int main() 
    { 
        int n,r;
        cin>>n>>r;
        int arr[n];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        int res=0;
        printCombination(arr, n, r); 
        for(int i=0;i<ans.size();i++)
        {
           cout<<ans[i]<<endl;
            
        }
        cout<<res<<endl;
    }