Why WA? Even when it matches editorial (almost)

Problem statement
https://www.codechef.com/CENS2020/problems/CENS20D

My soln… https://www.codechef.com/viewsolution/36993543

The problem is that when N = 1, you’re not reading the input array and that element is being read as the N for the next test case. So, you’re reading in the test cases wrong.

2 Likes

Line 25: for(int i=1; i<=n-1; i++)
Should be for(int i=1; i<=n; i++)

Same with line 27.
Also, like others suggested about missed input, the if-else branching is a good practice but won’t be much useful as execution probably won’t enter the for loops, provided loops are written correctly.

1 Like

Move the

 if(n==1)
	    {
	        cout<<"0"<<endl;
	        continue;
	    }

after the

for(int i=1; i<=n; i++) cin>>A[i];

otherwise you’ll miss reading in some input, and so end up reading in the wrong numbers!

Edit:

Beaten by @thatprakhar :slight_smile:

5 Likes

Okay :no_mouth:

Okay thankyou😑