Ctrl K

Sorted After One Fix

Medium
You are given an integer array values. Can it be made non-decreasing by changing at most one element (to any integer)? An array is non-decreasing when every element is at most the next one: values[i] ≤ values[i + 1] for every i from 0 to len(values) - 2. Return True if it can, and False otherwise.
Example 1
values=[3, 9, 5, 6] → True

Change the 9 to, say, 4 and the array becomes [3, 4, 5, 6].

Example 2
values=[6, 2, 7, 1] → False

There are two drops, 6 → 2 and 7 → 1, far enough apart that one change cannot fix both.

Example 3
values=[-4, -4, 0] → True
Constraints
  • 1 ≤ len(values) ≤ 10⁴
  • -10⁵ ≤ values[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Boards Holding Water MediumZero-Sum Triples MediumTriple Sum Nearest the Target MediumFour Numbers to a Target MediumNext Arrangement in Order Medium
loading Python…
Input
values=[3, 9, 5, 6]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 168 tests on the server, hidden ones included.