TRIP2 - Need help!

I attempted the AUG-LTIME and was stuck at the question : TRIP2
I am not able to catch my mistake . Please help me to understand why my approach fails.

Idea : Since question stated that we can visit any place . I tried to fill the -1 s with K
(max possible) and change the value only when needed . According to me it should have automatically handled the K=2 and K>=3 .

Let’s say we are at position i . If A[i] == -1 and previous is K , we assign K-1 . If next is also K-1 , we again reduce A[i] by 1 to ensure that it is not same.
If A[i] == -1 and previous is not K , we assign K and look at the next element to cross check .

If by reducing we obtain A[i] == 0 ( i.e K must have been 2 ) . We conclude that it is not possible .

Any help from this wonderful community would be greatly appreciated .

[](Link to code)

Here’s a failing testcase to help you debug:

7 2
-1 -1 1 -1 -1 -1 -1
1 Like