You are given a 0-indexed integer array nums. You are allowed to remove exactly one element from the array. Return true if it can be made strictly increasing after removing exactly one element, otherwise return false.
An array nums is strictly increasing if nums[i] < nums[i+1] for every index i where 0 <= i < nums.length - 1.
1 <= nums.length <= 10^4.true since removing any element will still maintain the order.1 or 2, as they are trivially strictly increasing after removing one element.true.is_strictly_increasing(arr) to check if a given subarray is strictly increasing.1 and 2 should always return true since they can’t violate the strictly increasing property.def can_be_increasing(nums):
def is_strictly_increasing(arr):
for i in range(len(arr) - 1):
if arr[i] >= arr[i + 1]:
return False
return True
n = len(nums)
if n <= 2:
return True
for i in range(n - 1):
if nums[i] >= nums[i + 1]:
# Try removing nums[i]
if is_strictly_increasing(nums[:i] + nums[i+1:]):
return True
# Try removing nums[i+1]
if is_strictly_increasing(nums[:i+1] + nums[i+2:]):
return True
return False
return True
# Example Test Cases
print(can_be_increasing([1,2,10,5,7])) # Expected True
print(can_be_increasing([2,3,1,2])) # Expected False
print(can_be_increasing([1,1,1])) # Expected False
print(can_be_increasing([1,2,3])) # Expected True
is_strictly_increasing function checks the array in O(n) time. As we might call this function twice in one traversal for an array of length n, the overall complexity is O(n).O(1) additional space since we are only creating a few variables and not using any extra space that scales with input size.This strategy efficiently identifies whether the array can be made strictly increasing by removing one element, ensuring a linear runtime.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?