Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element.
We define an 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 Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
Example 2:
Input: nums = [4,2,1] Output: false Explanation: You cannot get a non-decreasing array by modifying at most one element.
Constraints:
n == nums.length1 <= n <= 104-105 <= nums[i] <= 105Problem summary: Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element. We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[4,2,3]
[4,2,1]
make-array-non-decreasing-or-non-increasing)find-good-days-to-rob-the-bank)count-non-decreasing-subarrays-after-k-operations)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #665: Non-decreasing Array
class Solution {
public boolean checkPossibility(int[] nums) {
for (int i = 0; i < nums.length - 1; ++i) {
int a = nums[i], b = nums[i + 1];
if (a > b) {
nums[i] = b;
if (isSorted(nums)) {
return true;
}
nums[i] = a;
nums[i + 1] = a;
return isSorted(nums);
}
}
return true;
}
private boolean isSorted(int[] nums) {
for (int i = 0; i < nums.length - 1; ++i) {
if (nums[i] > nums[i + 1]) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #665: Non-decreasing Array
func checkPossibility(nums []int) bool {
isSorted := func(nums []int) bool {
for i, b := range nums[1:] {
if nums[i] > b {
return false
}
}
return true
}
for i := 0; i < len(nums)-1; i++ {
a, b := nums[i], nums[i+1]
if a > b {
nums[i] = b
if isSorted(nums) {
return true
}
nums[i] = a
nums[i+1] = a
return isSorted(nums)
}
}
return true
}
# Accepted solution for LeetCode #665: Non-decreasing Array
class Solution:
def checkPossibility(self, nums: List[int]) -> bool:
def is_sorted(nums: List[int]) -> bool:
return all(a <= b for a, b in pairwise(nums))
n = len(nums)
for i in range(n - 1):
a, b = nums[i], nums[i + 1]
if a > b:
nums[i] = b
if is_sorted(nums):
return True
nums[i] = nums[i + 1] = a
return is_sorted(nums)
return True
// Accepted solution for LeetCode #665: Non-decreasing Array
struct Solution;
impl Solution {
fn check_possibility(nums: Vec<i32>) -> bool {
let n = nums.len();
if n < 2 {
return true;
}
let mut p: Option<usize> = None;
for i in 0..n - 1 {
if nums[i] > nums[i + 1] {
if p.is_some() {
return false;
} else {
p = Some(i);
}
}
}
if let Some(p) = p {
if p != 0 && p != n - 2 {
if nums[p - 1] >= nums[p + 1] && nums[p] >= nums[p + 2] {
return false;
}
}
}
true
}
}
#[test]
fn test() {
assert_eq!(Solution::check_possibility(vec![4, 2, 3]), true);
assert_eq!(Solution::check_possibility(vec![4, 2, 1]), false);
}
// Accepted solution for LeetCode #665: Non-decreasing Array
function checkPossibility(nums: number[]): boolean {
const isSorted = (nums: number[]) => {
for (let i = 0; i < nums.length - 1; ++i) {
if (nums[i] > nums[i + 1]) {
return false;
}
}
return true;
};
for (let i = 0; i < nums.length - 1; ++i) {
const a = nums[i],
b = nums[i + 1];
if (a > b) {
nums[i] = b;
if (isSorted(nums)) {
return true;
}
nums[i] = a;
nums[i + 1] = a;
return isSorted(nums);
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.