Remove One Element to Make the Array Strictly Increasing - Problem

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).

Input & Output

Example 1 — Can Remove One Element
$ Input: nums = [1,2,10,5,7]
Output: true
💡 Note: Remove the element at index 2 (value 10) to get [1,2,5,7], which is strictly increasing: 1 < 2 < 5 < 7
Example 2 — Already Strictly Increasing
$ Input: nums = [2,3,1,2]
Output: false
💡 Note: No single removal can make this strictly increasing. Removing any one element still leaves violations
Example 3 — Already Strictly Increasing
$ Input: nums = [1,2,3]
Output: true
💡 Note: Already strictly increasing: 1 < 2 < 3, so return true

Constraints

  • 2 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Remove One Element to Make Array Strictly Increasing INPUT nums = [1, 2, 10, 5, 7] 1 i=0 2 i=1 10 i=2 5 i=3 7 i=4 Violation Found! 10 > 5 (not increasing) nums[2]=10, nums[3]=5 Goal: Can we remove exactly ONE element to make array strictly increasing? ALGORITHM STEPS 1 Initialize violations = 0, violationIdx = -1 2 Scan Array Check if nums[i-1] >= nums[i] 3 Count Violations If violation found, increment count 4 Try Removals Remove i or i-1, check result Processing: i=1: 1 < 2 [OK] i=2: 2 < 10 [OK] i=3: 10 > 5 [VIOLATION!] i=4: 5 < 7 [OK] Try removing 10: [1,2,5,7] 1<2<5<7 -- Strictly increasing! FINAL RESULT After removing element 10: 1 2 5 7 < < < Output: true Verification: Removed: 10 (index 2) Result: [1, 2, 5, 7] Strictly Increasing: [OK] Key Insight: When a violation nums[i-1] >= nums[i] is found, we have two choices: remove nums[i-1] or remove nums[i]. We check both options by verifying if the remaining array maintains strict increasing order. If violations > 1, return false immediately (can only remove ONE element). TutorialsPoint - Remove One Element to Make the Array Strictly Increasing | Single Pass Approach Time Complexity: O(n) | Space Complexity: O(1)
Asked in
Facebook 25 Google 20
23.4K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen