Non-decreasing Array

MediumArray

Description

Given an array nums with n integers, check if it could become non-decreasing by modifying at most one element. Non-decreasing means nums[i] <= nums[i+1]. Return true or false.

Examples

Input:nums = [4,2,3]
Output:true
Explanation:

Modify index 0: change 4 to 1 or 2, making [1,2,3] or [2,2,3]. Only one modification needed.

Input:nums = [4,2,1]
Output:false
Explanation:

For nums = [4,2,1], the answer is false because the non-decreasing array condition is not met.

Input:nums = [1,3,2,4]
Output:true
Explanation:

Modify the array by changing 3 to 2 (or 2 to 3), resulting in [1,2,2,4] which is non-decreasing. Only one modification is needed.

Constraints

  • 1 ≤ n ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!