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 n, int a[]){int cnt = 0;for (int i = 0; i < n; i++){if (i % 2 == 0)cnt += a[i];elsecnt -= a[i];}if (cnt == 0)cout << "YES\n";elsecout << "NO\n";}int main(){int n = 2;int a[n] = {1, 2};modifySequence(n, a);return 0;}
No comments:
Post a Comment