Can someone please explain the solution to this problem?
I have tried solving the problem. What I thought is that we can iterate from 1 to n, and for each A[I[, if it is less than A[i-1] and if there is no element in between yet, then we can increment the answer for the range (A[I-1], A[i]-1) by 1. However, I figured out, that’s the wrong approach. From there, i could not think further. I also tried reading the solution mentioned here.
however, I was not able to understand it. It would be helpful if someone could make me get through it in some simple words and details of each step.

Also, if you are mentioning any other solution, please care to provide proof of it.

Thank you in advance.