Here is the link to the problem:
Here is the code:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<set>
#include<vector>
#include<utility>
using namespace std;
int main( ) {
//numLeaves is the number of leaves input k times
//ctrNextLevel is the number of stems expected in the next level(leaf or child producing)
//numProducers is a temp variable used to calculate ctrNextLevel
int t, k, numLeaves, ctrNextLevel, numProducers;
//used as a flag if a wrong count is detected
bool countWrong;
cin>>t;
while ( t-- ) {
cin>>k;
countWrong = false;
for ( int i = 0; i < k; i++ ) {
scanf("%d", &numLeaves);
if ( countWrong == true ) {
continue;
}
// first level must have 1 or 0, anything else is wrong.
if ( i == 0 ) {
if ( numLeaves == 0 ) {
ctrNextLevel = 2;
}
else if ( numLeaves == 1 ) {
ctrNextLevel = 0;
}
else {
countWrong = true;
}
}
else { //for all levels after the first level
if ( numLeaves > ctrNextLevel ) {
countWrong = true;
}
else if ( numLeaves == ctrNextLevel ) {
ctrNextLevel = 0;
}
else {
numProducers = ctrNextLevel - numLeaves;
ctrNextLevel = numProducers * 2;
if ( i == ( k - 1 ) ) {
countWrong = true;
}
}
}
}
if ( countWrong == true ) {
cout<<"No"<<endl;
}
else {
cout<<"Yes"<<endl;
}
}
return 0;
}
return 0;
}