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

In this guide, you will get 1909. Remove One Element to Make the Array Strictly Increasing LeetCode Solution with the best time and space complexity. The solution to Remove One Element to Make the Array Strictly Increasing problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Remove One Element to Make the Array Strictly Increasing solution in C++
  4. Remove One Element to Make the Array Strictly Increasing solution in Java
  5. Remove One Element to Make the Array Strictly Increasing solution in Python
  6. Additional Resources
1909. Remove One Element to Make the Array Strictly Increasing LeetCode Solution image

Problem Statement of Remove One Element to Make the Array Strictly Increasing

Given a 0-indexed integer array nums, return true if it can be made strictly increasing after removing exactly one element, or false otherwise. If the array is already strictly increasing, return true.
The array nums is strictly increasing if nums[i – 1] < nums[i] for each index (1 <= i < nums.length).

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:

See also  3341. Find Minimum Time to Reach Last Room I LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1909. Remove One Element to Make the Array Strictly Increasing LeetCode Solution in C++

class Solution {
 public:
  bool canBeIncreasing(vector<int>& nums) {
    bool removed = false;

    for (int i = 1; i < nums.size(); ++i)
      if (nums[i - 1] >= nums[i]) {
        if (removed)
          return false;
        removed = true;  // Remove nums[i - 1].
        if (i > 1 && nums[i - 2] >= nums[i])
          nums[i] = nums[i - 1];  // Remove nums[i] instead.
      }

    return true;
  }
};
/* code provided by PROGIEZ */

1909. Remove One Element to Make the Array Strictly Increasing LeetCode Solution in Java

class Solution {
  public boolean canBeIncreasing(int[] nums) {
    boolean removed = false;

    for (int i = 1; i < nums.length; ++i)
      if (nums[i - 1] >= nums[i]) {
        if (removed)
          return false;
        removed = true; // Remove nums[i - 1].
        if (i > 1 && nums[i - 2] >= nums[i])
          nums[i] = nums[i - 1]; // Remove nums[i] instead.
      }

    return true;
  }
}
// code provided by PROGIEZ

1909. Remove One Element to Make the Array Strictly Increasing LeetCode Solution in Python

class Solution:
  def canBeIncreasing(self, nums: list[int]) -> bool:
    removed = False

    for i in range(1, len(nums)):
      if nums[i - 1] >= nums[i]:
        if removed:
          return False
        removed = True  # Remove nums[i - 1].
        if i > 1 and nums[i - 2] >= nums[i]:
          nums[i] = nums[i - 1]  # Remove nums[i] instead.

    return True
# code by PROGIEZ

Additional Resources

See also  2786. Visit Array Positions to Maximize Score LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.