left
and right
, to the start and end of the array respectively.left
forward until it points to the last element of the longest non-decreasing subarray starting from the beginning.right
backward until it points to the first element of the longest non-decreasing subarray from the end.result
to the minimum of the length of the array minus left - 1
and right
, as these represent the lengths of subarrays that can be removed from either end.i
starting from 0 and j
starting from right
. If arr[j]
is greater than or equal to arr[i]
, update result
to the minimum of result
and j - i - 1
, and increment i
. Otherwise, increment j
.i
exceeds left
or j
reaches the end of the array.result
as the length of the shortest subarray to remove.