Why am I getting WA (ZCO14002)

This is my code: https://ideone.com/UitcP6

I ain’t able to understand, why do I get WA, even though when thought about the approach, I find it correct!

Please help!


Hey @arnavvarshney

Your code fails for [this][1] test case. It should give output as 1 but it is giving 2.

Apart from that your code gives a runtime error if we take value of n as 1 or 2. ArrayIndexOutOfBoundsException will be thrown in these cases. [Example][2].


You can have a look at my


**Explanation:** The main thing to keep in mind is that out of any three consecutive numbers we have to choose atleast one number.

*Corner condition:* If the number of elements is less than 3, then we can directly return 0.

Now I have used an extra space array minTime. minTime[i] assumes that we include a[i] in our desired sum and since we are including a[i], then we just need to make sure that one of **(i-1)**,**(i-2)** or **(i-3)** should be taken into sum and thus we include the minimum of the sums of these three values. In this way, our minTime array will always have a minimum sum value after including a particular element at **i** index. At the end, we just need to compare the minTime values for the last three elements.

I may not be able to express the solution in words but the code might help in understanding the approach.

  [1]: https://ideone.com/CNuiBr
  [2]: https://ideone.com/jIChGx
  [3]: https://ideone.com/Kz0UPh

Hey @therisingsun

Thanks for answering the question…

If possible, could you also answer the following doubt??

In the example you gave, the answer would be 1??

Yes. The answer should be 1. The problem statement states that “The school’s rules say that no student can go three days in a row without any SUPW duty”. So in our example, Nikhil assigns himself the duty for the third day. In this way, he does not need to work on the first two days and the fourth day and thus he spends only 1 minute for duty.

1 Like

Please also help me with the other question…

Hey! @therisingsun

I thought about your suggestion, and made the following adjustments:


I still get a WA, with the code passing only 1 test case with AC, rest are WA!

Please help! Thanks!

Still 1 AC!


Just give me 10-15 minutes. Will solve the problem first and then try to explain you the solution.

1 Like

Edited the answer.
In case some more explanation is required, feel free to comment.