Modify Sequence

QUESTION

Suppose we have a sequence of non-negative integers, Namely a_1, a_2, … ,a_n. At each time we can choose one term a_i with 0 < i < n and we subtract 1 from both a_i and a_i+1. We wonder whether we can get a sequence of all zeros after several operations.\n\nInput\n\nThe first line of test case is a number N. (0 < N <= 10000) The next line is N non-negative integers, 0 <= a_i <= 109\n\nOutput\n\nIf it can be modified into all zeros with several operations output YES in a single line, otherwise output NO instead.

“TESTCASE_1”: “2\n1 2\n###—###SEPERATOR—###—\nNO”, “TESTCASE_2”: “2\n2 2\n###—###SEPERATOR—###—\nYES”, “TESTCASE_3”: “5\n5 5 5 5 5\n###—###SEPERATOR—###—\nNO”, “TESTCASE_4”: “1\n0\n###—###SEPERATOR—###—\nYES”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main() 
{
	lli n;
    cin>>n;
    vector<lli> arr(n,0);
    for(lli i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    lli c=1;
    for(lli i=0;i<n-1;i++)
    {
        if(arr[i]<0)
        {
            c=0;
            break;
        }
        arr[i+1] -= arr[i];
        arr[i]=0;
    }
    if(arr[n-1]!=0)
    {
        c=0;
    }
    if(c==1)
    {
        cout<<"YES\n";
    }
    else
        cout<<"NO\n";
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.