Sketch
First draw a quick sketch of the problem.

The sketch allows us to see how easy it is to calculate the distance from the ends using the left and right "end indexes" (0 and 6 for this specific example).

Of course we want to do this for the "general case" not just this one specific example.
Hint 1: Is there an algorithm (method) within our context that will provide much of the needed logic?

Hint 2: Is there a second algorithm (method) within our context that will also provide helpful logic?

If so, we should consider calling these two existing algorithms.
Reuse