Input: N=3, arr[] = {9, 8, 7}
Output: 3
Explanation: The good ranges are: (2, 3), (1, 3), (1, 2).
(2, 3) is a good range as the resultant array [9] is sorted (we deleted 8, 7).
(1, 3) is a good range as the resultant array [] which is sorted (we deleted 9, 8, 7)
(1, 2) is a good range as the resultant array [7] is sorted (we deleted 9, 8).
Input: N=5, arr[] = {5, 3, 1, 5, 2}
Output: 7
Explanation: The good ranges are: (1, 2), (1, 3), (1, 4), (1, 5), (2, 4), (2, 5), (3, 5).
(1, 2) is a good range as the resultant array [1, 2] is sorted
(1, 3) is a good range as the resultant array [2] is sorted
(1, 4) is a good range as the resultant array [2] is sorted
(1, 5) is a good range as the resultant array [] is sorted
(2, 4) is a good range as the resultant array [2] is sorted
(2, 5) is a good range as the resultant array [] is sorted
(3, 5) is a good range as the resultant array [3] is sorted
Below is the implementation of the above approach.