Piyush and Magical Park

Why i am getting wrong output ?

@vaishnavtannu

  1. If the strength were to become negative during the course of traversal , you need not proceed further and end your loops there only. Implement a check for if(s<0) inside your loops to account for this.
  2. In your final answer , case when the output is “Yes” , you are required to print the strength gained , not the final amount of strength left i.e. while traversing there were instances when you decreased your strength like s-=2 and s-=3 and there were instances when you increased it like s+=4 and s+=5. You are required to print the amount of strength you increased which would account for only s+=4 and s+=5 values and not the decremented values.

Can you please elaborate the 2nd point. I am unable to understand it.

@vaishnavtannu
In your final answer , you have print the value of ‘s’ i.e. the amount of strength left after completing the traversal. But that is not what the problem statement asks. The output format of the problem states - “print the maximum strength that he can gather in the park in a new line”. Hence we are required to print the amount of strength gained only.
Make a seperate variable , say
int strengthGained = 0 ;
When you perform the statement s+=4 or s+=5 , increment strengthGained by the same amount.
strengthGained+=4; //When you do s+=4
strengthGained+=5; //When you do s+=5

However do not decrease its value along with s-=2 or s-=3 statements.

Try this.

Note : You will find that your answers are off by one. This is due to the fact that you are doing all your computations for strength by counting the amount of strength required to leave the current cell. So in order to accomodate for it , you might have to initialise strengthGained by 1 instead of 0.

Give it a try.

Still I am getting wrong answer for the 1st testcase .

@vaishnavtannu
This might be for a case like this when there is no strength gained at all.
Input :
4 4 5 20
> # . * .
> # # . .
> # * . .
> # # * *

Expected Output :
Yes
0
(We never encountered any * hence no strength gained)

Your Output :
Yes
1