Conveyor Repair Window

A warehouse conveyor line carries n crates in a row. Every crate is stamped with a numeric priority code, and a healthy line reads those codes left to right in nondecreasing order. Overnight, a jammed robotic arm grabbed one contiguous stretch of the line and shuffled the crates inside it. That stretch may have been empty or may have spanned the whole line, but every crate outside it stayed frozen in place.

The day crew is allowed exactly one repair: lift the scrambled stretch, sort its codes ascending, and set it back down. No other crate may move. Return the minimum number of crates that must take part in such a repair so the whole line reads nondecreasing again. If the line was never disturbed, return 0.

Formally, given an integer array nums of length n, find the shortest contiguous subarray whose ascending sort makes the entire array nondecreasing, and return its length.

Examples
Input: [5,8,6,6,9,12]
Output: 3
Hints

Conveyor Repair Window

A warehouse conveyor line carries n crates in a row. Every crate is stamped with a numeric priority code, and a healthy line reads those codes left to right in nondecreasing order. Overnight, a jammed robotic arm grabbed one contiguous stretch of the line and shuffled the crates inside it. That stretch may have been empty or may have spanned the whole line, but every crate outside it stayed frozen in place.