this is because we are dividing the questions into two parts and take min of both
We can divide the questions into two parts i.e. in the first part we will consider only left elements (j<=ij<=i) for calculating sold values and in the second part we will consider only right elements (j>=ij>=i) for sold values. We will maintain two dps for each part and then find the minimum of them.
- For calculating left_dp iterate through 22 to NN calculating minimum value as min(A[i],left_dp[i-1]+1).
- For calculating right_dp, iterate through N-1 to 1 calculating minimum value as min(A[i],right_dp[i+1]+1).
And now minimum value of left_dp and right_dp will be the answer for each index.