2

I know this code/logic is wrong for solving the subset sum problem, but can't seem to understand why.

Calculate the sum of all possible subsets and check if any is equal to the required sum. This would be done in O(n^2) which is obviously wrong as I can solve this via DP O(n*sum).

Thank you.

int main() {
    long long int t,n,i,j;
    scanf("%lld",&t);
    while(t--)
    {   
        long long int p[1005][1005],s[1005][1005]={0};
        long long int a[1005],sum;
        long long int counter=0;
        scanf("%lld %lld",&n,&sum);
        for(i=0;i<n;i++)
            scanf("%lld",&a[i]);
        s[0][0]=a[0];
        for(i=0;i<n;i++)
        {   
            for(j=i;j<n;j++)
            {
                if(i==j)
                {
                    s[i][j]=a[i];
                }
                else
                {
                    s[i][j]=a[j]+s[i][j-1];
                }
            }
        }
        int flag=0;
        for(i=0;i<n;i++)
        {
            for(j=i;j<n;j++)
            {
                if(s[i][j]==sum)
                    flag++;
            }
        }
        if(flag)
            cout<<1<<endl;
        else
            cout<<0<<endl;

    }
    return 0;
}  
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
someone1
  • 115
  • 1
  • 11
  • 2
    Also, Subset sum is NP complete. If you can solve it in faster than exponential time for all problem instances, then you would win all kinds of prizes. – AndyG Dec 21 '15 at 18:51
  • I know it's wrong, I'm just asking for the flaw in the logic. – someone1 Dec 21 '15 at 18:57
  • 1
    Have you read https://en.wikipedia.org/wiki/Subset_sum_problem – Support Ukraine Dec 21 '15 at 18:58
  • I have added the code. I have properly interpreted the problem wrong, just trying to understand the mistake. I know about the problem and that it is NP complete, and how to do it using Recursion and DP. Just a random doubt I had. – someone1 Dec 21 '15 at 18:59
  • What exactly is the question? Is the output of the implementation other than expected or is it unclear how to bound the running time? – Codor Dec 21 '15 at 19:05
  • Logically, I am calculating the sum of all possible subsets and then comparing and checking if any subset is equal to the required sum. When I run this with some random test cases, it works fine, when I submitted it on a practice problem on codechef, it got WA. As I think the running time would be O(n^2) (fairly new to Big O notation), and the problem is obviously NP complete, so I know my solution is wrong. What I'm wondering is, where is the error. Possibly a test case for which this fails? – someone1 Dec 21 '15 at 19:07
  • What is the exact meaning of s[i][j]? – notbad Dec 21 '15 at 19:14

2 Answers2

0

The problem with your code is simply that you are not calculating the sum of all subsets. That's why your code looks much faster than what it really takes.

See https://en.wikipedia.org/wiki/Subset_sum_problem

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • Thank you! A possible test case for which this would fail so I could understand better? – someone1 Dec 21 '15 at 19:08
  • @someone1 - There are 2^N subsets - you are only calculating N^2 subsets. Example: Assume N=20. Where do yo calculate the sum of the subset consisting of element number 2, 3, 5, 7, 11, 13, 17 and 19 ? You don't. – Support Ukraine Dec 21 '15 at 19:12
  • Ah! Thank you so much. I just realised the problem I had solved this for mentioned subARRAYs, so I only calculated the continuous subsets! Thank you so much, a very stupid question, I should have tried a bit more myself. Sorry for the trouble. – someone1 Dec 21 '15 at 19:17
0

The problem is s[i][j] just records a[i]+a[i+1]+...+a[j]. In fact, you need to record the sums for all subsets of a[i...j], which should be 2^(j-i). You code should fail easily if you targeted sum is not a sum of a continuous subset.

notbad
  • 2,797
  • 3
  • 23
  • 34