LeetCode 1909: Remove One Element to Make the Array Strictly Increasing Solution

Master LeetCode problem 1909 (Remove One Element to Make the Array Strictly Increasing), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1909. Remove One Element to Make the Array Strictly Increasing

Easy

Problem Explanation

Explanation

To solve this problem, we can iterate through the given array nums and for each element, we try removing it and check if the resulting array is strictly increasing. We can do this by maintaining two flags: removed to track if we have already removed an element, and strictlyIncreasing to check if the array is strictly increasing after removal.

We then iterate through the array and check if removing the current element makes the array strictly increasing. If it does, we set removed flag to true. If we encounter a violation of the strictly increasing condition, we return false. After the loop, if we haven't removed any element, we return false as well.

The time complexity of this approach is O(n) where n is the length of the input array nums, and the space complexity is O(1).

Solution Code

class Solution {
    public boolean canBeIncreasing(int[] nums) {
        boolean removed = false;
        boolean strictlyIncreasing = true;
        
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] <= nums[i - 1]) {
                if (removed) {
                    strictlyIncreasing = false;
                    break;
                }
                
                removed = true;
                
                if (i > 1 && nums[i] <= nums[i - 2]) {
                    nums[i] = nums[i - 1];
                }
            }
        }
        
        return strictlyIncreasing;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1909 (Remove One Element to Make the Array Strictly Increasing)?

This page provides optimized solutions for LeetCode problem 1909 (Remove One Element to Make the Array Strictly Increasing) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1909 (Remove One Element to Make the Array Strictly Increasing)?

The time complexity for LeetCode 1909 (Remove One Element to Make the Array Strictly Increasing) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1909 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1909 in Java, C++, or Python.

Back to LeetCode Solutions