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

1
2
3
4
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

1
2
3
4
5
6
7
8
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

1
2
3
4
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 <= 1000
  • 1 <= 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

  1. Iterate through the array and find the first index i where nums[i-1] >= nums[i].
  2. Try removing nums[i-1] and check if the rest is strictly increasing.
  3. Try removing nums[i] and check if the rest is strictly increasing.
  4. If either works, return true. If no violation, return true.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#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;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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).