Skip to main content

Minimum Prefix Removal to Make Array Strictly Increasing - Solution & Explanation

MediumArray6 min readAsked at: Jpmorgan
Practice this problem

Problem Statement

You are given an integer array nums.

You need to remove exactly one prefix (possibly empty) from nums.

Return an integer denoting the minimum length of the removed prefix such that the remaining array is strictly increasing.

 

Example 1:

Input: nums = [1,-1,2,3,3,4,5]

Output: 4

Explanation:

Removing the prefix = [1, -1, 2, 3] leaves the remaining array [3, 4, 5] which is strictly increasing.

Example 2:

Input: nums = [4,3,-2,-5]

Output: 3

Explanation:

Removing the prefix = [4, 3, -2] leaves the remaining array [-5] which is strictly increasing.

Example 3:

Input: nums = [1,2,3,4]

Output: 0

Explanation:

The array nums = [1, 2, 3, 4] is already strictly increasing so removing an empty prefix is sufficient.

 

Constraints:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109​​​​​​​

Approach Overview

Problem Overview: You are given an integer array and can remove any number of elements from the prefix. The goal is to remove the smallest prefix so the remaining suffix becomes strictly increasing. Formally, find the minimum k such that nums[k:] satisfies nums[i] < nums[i+1] for every valid index.

Approach 1: Brute Force Prefix Check (O(n^2) time, O(1) space)

Try every possible prefix removal. For each index k from 0 to n-1, check whether the suffix nums[k:] is strictly increasing. The check requires iterating through the remaining array and verifying nums[i] < nums[i+1]. The first valid k is the answer. This approach directly models the problem but performs repeated scans of the array, leading to O(n^2) time in the worst case. Space stays O(1) since no extra structures are required. Useful for reasoning about the problem, but inefficient for large inputs.

Approach 2: Reverse Traversal (O(n) time, O(1) space)

The key observation: if a suffix is strictly increasing, every pair of adjacent elements inside it must satisfy nums[i-1] < nums[i]. Instead of testing every prefix removal, scan the array from the right and locate the longest strictly increasing suffix. Start at the last index and move left while the increasing condition holds. The moment you encounter a violation (nums[i-1] >= nums[i]), the suffix beginning at i is the longest valid strictly increasing suffix. Removing the prefix of length i makes the remaining array strictly increasing.

This works because any earlier start would include the violating pair, breaking the strictly increasing property. The scan touches each element at most once, giving O(n) time with constant extra memory. The logic is simple: iterate backward and track where the increasing order breaks.

Conceptually, this solution relies on properties of arrays and a single linear pass similar to techniques used in two pointers style scans where order relationships between neighbors determine valid segments.

Recommended for interviews: Reverse Traversal. Interviewers expect the O(n) observation that only the longest increasing suffix matters. Showing the brute force idea first demonstrates problem understanding, while the reverse scan shows optimization and pattern recognition.

Solution

We can traverse the array backwards from the end to find the first position i that does not satisfy the strictly increasing condition, i.e., nums[i-1] geq nums[i]. At this point, the minimum length of the prefix to remove is i.

If the entire array is strictly increasing, we do not need to remove any prefix, so we return 0.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Prefix CheckO(n^2)O(1)Initial reasoning or very small arrays where simplicity matters
Reverse Traversal (Longest Increasing Suffix)O(n)O(1)Optimal approach for interviews and large arrays

Video Solution

Minimum Prefix Removal to Make Array Strictly Increasing | Leetcode 3818 | Weekly Contest 486 • Developer Coder • 284 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Prefix Removal to Make Array Strictly Increasing easy or hard?
The problem is usually classified as Medium difficulty. The brute force idea is straightforward, but identifying that the answer depends on the longest strictly increasing suffix requires recognizing a pattern that leads to the optimal O(n) solution.
Minimum Prefix Removal to Make Array Strictly Increasing Python/Java solution
The typical implementation performs a reverse loop starting from the last index and moves left while nums[i-1] < nums[i]. Once the condition fails, return the current index as the number of elements to remove. The same logic works in Python, Java, C++, Go, and TypeScript with O(n) time and constant space.
How to solve Minimum Prefix Removal to Make Array Strictly Increasing in O(n)?
Traverse the array from the last element toward the beginning while the strictly increasing condition nums[i-1] < nums[i] holds. Stop when a violation appears. The suffix starting at that position is already strictly increasing, so removing the prefix up to that index produces the valid result. The traversal touches each element once, giving O(n) time.
What is the best approach for Minimum Prefix Removal to Make Array Strictly Increasing?
Reverse traversal is the most efficient approach. Scan from the end of the array and find the longest strictly increasing suffix where nums[i-1] < nums[i]. The index where this property first breaks determines how many prefix elements must be removed. This runs in O(n) time and O(1) space.
Is Minimum Prefix Removal to Make Array Strictly Increasing asked at Google/Amazon/Meta?
Problems involving strictly increasing subarrays, suffix scans, and array order validation frequently appear in interviews at companies like Amazon, Google, and Meta. Variants of this problem test whether candidates can identify monotonic segments and optimize from brute force to a linear scan.
What data structure is used in Minimum Prefix Removal to Make Array Strictly Increasing?
The solution operates directly on arrays. No additional data structures such as hash maps or stacks are required because the property of strict ordering can be verified by comparing adjacent elements during a single traversal.
What is the time complexity of Minimum Prefix Removal to Make Array Strictly Increasing?
The optimal solution runs in O(n) time because the array is scanned once from right to left. Each element is checked against its neighbor to verify the strictly increasing condition. Space complexity is O(1) since the algorithm only uses a few variables.

Ready to solve this problem?

Practice Minimum Prefix Removal to Make Array Strictly Increasing with our built-in code editor and test cases.

Practice on FleetCode