Input: arr = [2, 10, 7, 5, 4, 1, 8, 6]
Output: 5
Explanation: The minimum element is 1 and the maximum element is 10. We can visualise the deletion operations as below:
[2, 10, 7, 5, 4, 1, 8, 6]
[2, 10, 7, 5, 4, 1, 8]
[2, 10, 7, 5, 4, 1]
[2, 10, 7, 5, 4]
[10, 7, 5, 4]
[7, 5, 4]
Total 5 deletion operations performed. There is no other sequence with less deletions in which the minimum and maximum can be deleted.
Input: arr = [56]
Output: 1
Explanation: Because the array only has one entry, it serves as both the lowest and maximum value. With a single delete, we can eliminate it.
Input: arr = [2, 5, 8, 3, 6, 4]
Output: 3
Explanation: The minimum element is 2 and the maximum element is 8. We can visualise the deletion operations as below:
[2, 5, 8, 3, 6, 4]
[5, 8, 3, 6, 4]
[8, 3, 6, 4]
[3, 6, 4]
Total 3 deletions are performed. It is the minimum possible number of deletions.
Suppose the max and min elements exists at index i and j, or vice versa, as shown below:
Using the above equations we can now easily get distances using the index of min and max element. The answer is minimum of these 3 cases