Modify Sequence

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.

Example:

Input:  n = 2, a = [1,2]
Output: NO

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void modifySequence(int nint a[])
{
    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        if (i % 2 == 0)
            cnt += a[i];
        else
            cnt -= a[i];
    }
    if (cnt == 0)
        cout << "YES\n";
    else
        cout << "NO\n";
}
int main()
{
    int n = 2;
    int a[n] = {12};

    modifySequence(n, a);

    return 0;
}


No comments:

Post a Comment