-1
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
bool check(int a[], int n, int s)
{
    bool dp[n+1][s+1];
    for(int i=0;i<n+1;i++)
    {
        for(int j=0;j<s+1;j++)
        {
            if(i==0)
                dp[i][j] = false;
            if(j==0)
                dp[i][j] = true;
            if(a[i-1]<=j)
                dp[i][j] = dp[i-1][j-a[i-1]] || dp[i-1][j];
            else
                dp[i][j] = dp[i-1][j];
        }
    }
    return dp[n][s];
}
int main()
 {
    //code
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        int s=0;
        for(int i=0;i<n;i++){
            cin>>arr[i];
            s+=arr[i];
        }
        if(s%2==0)
        {
            s/=2;
            if(check(arr,n,s))
                cout<<"YES\n";
            else
                cout<<"NO\n";
        }
        else
            cout<<"NO\n";
    }
    return 0;
}

The question is Given a set of numbers, check whether it can be partitioned into two subsets such that the sum of elements in both subsets is same or not.

For a test-case; 4 1 5 11 5 The output will be YES because There exists two subsets such that {1, 5, 5} and {11}.

The above test case is passed but it is showing wrong answer for 8 479 758 315 472 730 101 460 619 The actual answer should be YES but it is showing NO.

  • 3
    `#include` -- [no, please](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). `using namespace std;` -- [no, please](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). `#define ll long long` -- no, please. (You're not actually *using* that define in the first place, and it's making your code *less* readable, not more so.) `int arr[n];` -- [no, please](https://stackoverflow.com/questions/39334435/variable-length-array-vla-in-c-compilers). – DevSolar Aug 19 '20 at 11:12
  • 1
    Generic debugging advice, *check your assumptions, check your intermediate values*. Add some debug output and find where your results start deviating from your assumptions. It appears you haven't done any of that yet, since you present your code as a monolith and expect us to debug it. – DevSolar Aug 19 '20 at 11:24
  • Try to use a debugger. Or at least add some comments into your code. – tibi Aug 19 '20 at 11:25
  • the first two `if` look wrong. Whatever is assigned to `dp[i][j]` will be overwritten by the `if - else` that follows. Can only join the previous commenters, using a debugger would reveal such mistakes easily – 463035818_is_not_an_ai Aug 19 '20 at 11:37
  • Thank you everyone. The problem now is solved. – PRAJWAL BHAGAT Aug 19 '20 at 11:38

1 Answers1

-2
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
bool check(int a[], int n, int s)
{
    bool dp[n+1][s+1];
    for(int i=0;i<n+1;i++)
    {
        for(int j=0;j<s+1;j++)
        {
            if(i==0)
                dp[i][j] = false;
            else if(j==0)
                dp[i][j] = true;
            else if(a[i-1]<=j)
                dp[i][j] = dp[i-1][j-a[i-1]] || dp[i-1][j];
            else
                dp[i][j] = dp[i-1][j];
        }
    }
    return dp[n][s];
}
int main()
 {
    //code
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        int s=0;
        for(int i=0;i<n;i++){
            cin>>arr[i];
            s+=arr[i];
        }
        if(s%2==0)
        {
            s/=2;
            if(check(arr,n,s))
                cout<<"YES\n";
            else
                cout<<"NO\n";
        }
        else
            cout<<"NO\n";
    }
    return 0;
}

This is the corrected code.

  • You have not explained what you fixed, **and** you have not heeded any of the advice given in the comments. At the very least your code is still non-compliant due to the use of variable-length arrays in C++, and in very poor style due to the `` include. – DevSolar Aug 19 '20 at 21:44