Remove One Element to Make the Array Strictly Increasing
EasyUpdated: Aug 2, 2025
Practice on:
Problem
Given a 0-indexed integer array nums, return true _if it can be madestrictly increasing after removing exactly one element, or _false otherwise. If the array is already strictly increasing, returntrue.
The array nums is strictly increasing if nums[i - 1] < nums[i] for each index (1 <= i < nums.length).
Examples
Example 1
Input: nums = [1,2,_10_ ,5,7]
Output: true
Explanation: By removing 10 at index 2 from nums, it becomes [1,2,5,7].
[1,2,5,7] is strictly increasing, so return true.
Example 2
Input: nums = [2,3,1,2]
Output: false
Explanation:
[3,1,2] is the result of removing the element at index 0.
[2,1,2] is the result of removing the element at index 1.
[2,3,2] is the result of removing the element at index 2.
[2,3,1] is the result of removing the element at index 3.
No resulting array is strictly increasing, so return false.
Example 3
Input: nums = [1,1,1]
Output: false
Explanation: The result of removing any element is [1,1].
[1,1] is not strictly increasing, so return false.
Constraints
2 <= nums.length <= 10001 <= nums[i] <= 1000
Solution
Method 1 - Greedy Check for One Violation
Intuition
If the array is not strictly increasing, there must be at most one place where nums[i-1] >= nums[i]. We can try removing either nums[i-1] or nums[i] and check if the result is strictly increasing.
Approach
- Iterate through the array and find the first index i where nums[i-1] >= nums[i].
- Try removing nums[i-1] and check if the rest is strictly increasing.
- Try removing nums[i] and check if the rest is strictly increasing.
- If either works, return true. If no violation, return true.
Code
C++
#include <vector>
using namespace std;
bool isStrict(const vector<int>& nums, int skip) {
int prev = -1;
for (int i = 0; i < nums.size(); ++i) {
if (i == skip) continue;
if (prev != -1 && nums[prev] >= nums[i]) return false;
prev = i;
}
return true;
}
bool canBeIncreasing(vector<int>& nums) {
for (int i = 1; i < nums.size(); ++i) {
if (nums[i-1] >= nums[i]) {
return isStrict(nums, i-1) || isStrict(nums, i);
}
}
return true;
}
Go
func isStrict(nums []int, skip int) bool {
prev := -1
for i := 0; i < len(nums); i++ {
if i == skip {
continue
}
if prev != -1 && nums[prev] >= nums[i] {
return false
}
prev = i
}
return true
}
func canBeIncreasing(nums []int) bool {
for i := 1; i < len(nums); i++ {
if nums[i-1] >= nums[i] {
return isStrict(nums, i-1) || isStrict(nums, i)
}
}
return true
}
Java
public class Solution {
public boolean canBeIncreasing(int[] nums) {
for (int i = 1; i < nums.length; i++) {
if (nums[i-1] >= nums[i]) {
return isStrict(nums, i-1) || isStrict(nums, i);
}
}
return true;
}
private boolean isStrict(int[] nums, int skip) {
int prev = -1;
for (int i = 0; i < nums.length; i++) {
if (i == skip) continue;
if (prev != -1 && nums[prev] >= nums[i]) return false;
prev = i;
}
return true;
}
}
Kotlin
fun canBeIncreasing(nums: IntArray): Boolean {
fun isStrict(skip: Int): Boolean {
var prev = -1
for (i in nums.indices) {
if (i == skip) continue
if (prev != -1 && nums[prev] >= nums[i]) return false
prev = i
}
return true
}
for (i in 1 until nums.size) {
if (nums[i-1] >= nums[i]) {
return isStrict(i-1) || isStrict(i)
}
}
return true
}
Python
def canBeIncreasing(nums):
def is_strict(skip):
prev = None
for i in range(len(nums)):
if i == skip:
continue
if prev is not None and nums[prev] >= nums[i]:
return False
prev = i
return True
for i in range(1, len(nums)):
if nums[i-1] >= nums[i]:
return is_strict(i-1) or is_strict(i)
return True
Rust
fn can_be_increasing(nums: &[i32]) -> bool {
let n = nums.len();
let is_strict = |skip: usize| {
let mut prev: Option<usize> = None;
for i in 0..n {
if i == skip { continue; }
if let Some(p) = prev {
if nums[p] >= nums[i] { return false; }
}
prev = Some(i);
}
true
};
for i in 1..n {
if nums[i-1] >= nums[i] {
return is_strict(i-1) || is_strict(i);
}
}
true
}
TypeScript
function canBeIncreasing(nums: number[]): boolean {
function isStrict(skip: number): boolean {
let prev: number | undefined = undefined;
for (let i = 0; i < nums.length; i++) {
if (i === skip) continue;
if (prev !== undefined && nums[prev] >= nums[i]) return false;
prev = i;
}
return true;
}
for (let i = 1; i < nums.length; i++) {
if (nums[i-1] >= nums[i]) {
return isStrict(i-1) || isStrict(i);
}
}
return true;
}
Complexity
- ⏰ Time complexity: O(N^2), where N is the length of nums (can be optimized to O(N), but this is simple and sufficient for N <= 1000).
- 🧺 Space complexity: O(1).