NOCHANGE - No Change - SPOJ Solution C++

  Problem Link : NOCHANGE 


👉 Hint : Simple 2D DP

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int amt;
    cin>>amt;
    int n;
    cin>>n;
    int arr[n+1];
    arr[0]=0;
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
        arr[i]+=arr[i-1];
    }
    bool dp[n+1][amt+1];
    memset(dp,true,sizeof(dp));
    for(int i=0;i<amt+1;i++)
        dp[0][i]=false;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=amt;j++)
        {
            dp[i][j]=dp[i-1][j];
            if(arr[i]<=j)
                dp[i][j]=dp[i][j]||dp[i][j-arr[i]];
        }
    }
    
    if(dp[n][amt]==0)
        cout<<"NO";
       else
       cout<<"YES";
    return 0;
} 

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!