1 / 2
Dec 2023

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

int main()
{
int t;
cin >> t;
while(t–)
{
int n;
cin >> n;
int arr[n];
int x[n-1];
arr[0] = 1;

	for(int i= 0; i<n-1; i++)
	{
		cin >> x[i];
	}

	for(int i= 0; i< n-1; i++)
	{
		if(x[i] == 0)
		{
			arr[i + 1] = arr[i];
		}

		else if(x[i] == 1)
		{
			arr[i + 1] = arr[i] + 1;
		}

		else if(x[i] == 2)
		{
			arr[i+1] = arr[i] - 1;
		}
	}

	int min = arr[0];
	for(int i= 0; i<n; i++)
	{
		if(arr[i] < min)
		{
			min = arr[i];
		}
	}

	int diff;

	if(min < 1)
	{
		diff = 1 - min;
		for(int i=0; i<n; i++)
		{
			arr[i] += diff;
		}
	}

	for(int i = 0; i<n; i++)
	{
		cout << arr[i] << " ";
	}

	cout << endl;

}

return 0;

}

  • created

    Dec '23
  • last reply

    Dec '23
  • 1

    reply

  • 419

    views

  • 2

    users

  • 1

    link

I don’t know. I’ve just tried my own solution, and cannot get AC myself either.

I did wonder whether the trailing space after the final array element could be the reason, but still WA when I removed it.

I also wondered whether there could be additional values after the valid X[i]s in the input, but I don’t think there are.

There are an awful lot of WAs though, so I’m still suspicious of the input.

MOZSATLA5