We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. All Contests
  2. ProjectEuler+
  3. Project Euler #201: Subsets with a unique sum
  4. Discussions

Project Euler #201: Subsets with a unique sum

Problem
Submissions
Leaderboard
Discussions

Sort 28 Discussions, By:

recency

Please Login in order to post a comment

  • APovalyaev
    1 year ago+ 0 comments

    why sum=15 (8+1+6) is not included in the unique sums set (in the example)?

    0|
    Permalink
  • ramprakashbhagw1
    3 years ago+ 0 comments

    Please find error in the code c#

    using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) {

        int[] ar = Array.ConvertAll(Console.ReadLine().Split(' '), arTemp => Convert.ToInt32(arTemp));
        long[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arTemp => Convert.ToInt64(arTemp));
        long sum = 0;
        List<long> array = new List<long>();
            recorsiveSum(arr, 0, 0, 0, ar[1], ref array);
            var list= array.GroupBy(x => x)
               .Where(g => g.Count() == 1)
               .Select(y => y.Key)
               .ToList();
            sum = list.Sum();
            Console.WriteLine(sum);
    
    }
    
     static void  recorsiveSum(long[] arr, int ind, long sum,int m,int len, ref List<long> array)
        {
            long sum1 = 0;
            if (m == len)
            {
    
                m = 0;
                    array.Add(sum);
            }
            else
            {
                m++;
            }
    
            for (int i = ind; i < arr.Length; i++)
            {
                sum1 = sum;
                sum1 += arr[ind];
                ind++;
                recorsiveSum(arr, ind, sum1, m, len, ref array);
            }
    
        }
    

    }

    -1|
    Permalink
  • calvinkhoozhenc1
    4 years ago+ 0 comments

    Cannot contain duplicate??... Maybe

    2|
    Permalink
  • hsugu19
    4 years ago+ 0 comments

    I simplified the code.but stil showing timeout error for few test cases.anybody have an idea?

    from itertools import combinations sm_r=set() sm_l=set() start=list(map(int,input().split())) n,m=start[0],start[1] array=list(map(int,input().split())) c_l=list(combinations(array,m))

    for pair in c_l: sm=sum(list(pair)) if sm not in sm_l: sm_l.add(sm) else: sm_r.add(sm) print(sum(sm_l.difference(sm_r)))

    -1|
    Permalink
  • Anonympous
    4 years ago+ 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;
    }
    
    -2|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature