Back to DSA sheet

Non Decreasing Array

MediumArrays

Decide whether the array can be made non-decreasing (each element <= the next) by modifying at most one element.

Input format (stdin): the first line has n. The second line has n integers. Output true or false.

Examples
Input: 3 4 2 3
Output: true
Change 4 to 1 (or 2) to fix it.
Input: 3 4 2 1
Output: false
Two changes would be needed.
Constraints
  • 1 <= n <= 10^4
  • -10^5 <= nums[i] <= 10^5
Sheets
NeetCode 250
non-decreasing-array.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.