Shellhead96's blog

By Shellhead96, history, 6 years ago, In English

Probably the easiest problem I tried to approach it in different way by AP sum. Like for each step our total sum will decrease by 2 and finally stops when we reach 0 sum.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    cin>>n;
    long long int arr[n];
    long long int sum = 0;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        sum+=arr[i];
    }
    /*if(sum==0)
    {
        cout<<"YES"<<endl;
        return 0;
    }*/
    long long int x = 0;
    sum = sum+2;
    x = sum/2;
    if(sum%2==0)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    return 0;
    
}

I am failing at 3 test case. Is this the wrong method or I am doing something wrong.. Thanks in advance

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
  Vote: I like it +1 Vote: I do not like it

The algorithm is incorrect, consider input

2
2 4

The sequence can't be modified into all zeros, but (sum of elements + 2) is even and your algorithm outputs YES.