Non-decreasing Array

Given an array nums with n integers, check whether it could become non-decreasing by modifying at most one element. The array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that 0 <= i <= n - 2.

30:00

Non-decreasing Array
medium
Free written solution
Topics
Companies

Given an array nums with n integers, check whether it could become non-decreasing by modifying at most one element. The array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that 0 <= i <= n - 2.

Example 1:
Input: {"nums":[4,2,3]}
Output: true
Constraints:
  • n=nums.lengthn = \text{nums.length}

  • 1≤n≤1041 \leq n \leq 10^4

  • −105≤nums[i]≤105-10^5 \leq \text{nums}[i] \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[4,2,3]}

One repair token is available. Walk the array and spend it only where a bar is lower than the bar to its left.

repair token:

unused

nums: blue = checked and in order, orange = i, purple = i - 1, teal = i - 2

4

2

3

0

1

2

if nums[i-2] <= nums[i]

lower nums[i-1]

else

raise nums[i]

Variables
VariableValue
i-
fixes0
DepthFunction Call
Stack empty
0/7