Remove the smallest possible prefix so the remaining array becomes strictly increasing.
Given an integer array, remove a prefix of length (possibly $0$ or the entire array) so that the remaining suffix is strictly increasing. Return the minimum possible prefix length.
A sequence is strictly increasing if every element is greater than the one before it.
The task is to find the earliest point where the array can be cut so that the suffix from that point onward is strictly increasing.
Input Format
- An integer array
nums. - You may remove a prefix
nums[0..k-1]for somekin the range0 <= k <= nums.length. - Return the minimum
ksuch thatnums[k..]is strictly increasing.
Output Format
- Return one integer: the minimum prefix length to remove.
Constraints
1 <= nums.length- Values may be negative or positive integers.
- The remaining suffix may have length
0or1, which is always strictly increasing by definition.
Example 1
Input
nums = [1,2,3,2,4,5]
Output
3
Explanation
Removing the prefix [1,2,3] leaves [2,4,5], which is strictly increasing. No shorter prefix works.
Example 2
Input
nums = [5,4,3,2,1]
Output
4
Explanation
Removing the first 4 elements leaves [1], which is strictly increasing. Any shorter suffix has a decrease.
Show 1 more example
Example 3
Input
nums = [1,2,3]
Output
0
Explanation
The array is already strictly increasing, so no removal is needed.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.