KatInterview

665. Non-decreasing Array

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to determine if an array of integers can be made non-decreasing by modifying at most one element. A non-decreasing array is one where nums[i] <= nums[i+1] for all i from 0 to n-2.

Solution

Non-decreasing Array in Python

class Solution:
    def checkPossibility(self, nums: List[int]) -> bool:
        modifications = 0
        for i in range(len(nums) - 1):
            if nums[i] > nums[i+1]:
                # If we have already made a modification, we can't make another one.
                if modifications == 1:
                    return False
                modifications += 1

                # We have a violation at index i: nums[i] > nums[i+1].
                # We need to decide whether to modify nums[i] or nums[i+1].
                # To keep the array non-decreasing, we prefer lowering nums[i].
                # This is possible if nums[i-1] <= nums[i+1] (or if i is 0).
                # Let's check the condition where we CAN'T lower nums[i].
                # This happens when i > 0 and nums[i-1] > nums[i+1].
                # In this case, we MUST raise nums[i+1].
                if i > 0 and nums[i-1] > nums[i+1]:
                    nums[i+1] = nums[i]
                # Otherwise, we can lower nums[i]. For example, by setting nums[i] = nums[i+1].
                # This change doesn't need to be coded because the element at nums[i]
                # won't be a part of any future nums[k] > nums[k+1] checks where k >= i.

        return True

Non-decreasing Array in Java

class Solution {
    public boolean checkPossibility(int[] nums) {
        int modifications = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] > nums[i + 1]) {
                // If we have already made a modification, we can't make another one.
                if (modifications == 1) {
                    return false;
                }
                modifications++;

                // We have a violation at index i: nums[i] > nums[i+1].
                // We need to decide whether to modify nums[i] or nums[i+1].
                // To keep the array non-decreasing, we prefer lowering nums[i].
                // This is possible if nums[i-1] <= nums[i+1] (or if i is 0).
                // Let's check the condition where we CAN'T lower nums[i].
                // This happens when i > 0 and nums[i-1] > nums[i+1].
                // In this case, we MUST raise nums[i+1].
                if (i > 0 && nums[i - 1] > nums[i + 1]) {
                    nums[i + 1] = nums[i];
                } 
                // Otherwise, we can lower nums[i]. For example, by setting nums[i] = nums[i+1].
                // This change doesn't need to be coded because the element at nums[i]
                // won't be a part of any future nums[k] > nums[k+1] checks where k >= i.
            }
        }
        return true;
    }
}

Non-decreasing Array in C++

class Solution {
public:
    bool checkPossibility(vector<int>& nums) {
        int modifications = 0;
        for (int i = 0; i < nums.size() - 1; ++i) {
            if (nums[i] > nums[i + 1]) {
                // If we have already made a modification, we can't make another one.
                if (modifications == 1) {
                    return false;
                }
                modifications++;

                // We have a violation at index i: nums[i] > nums[i+1].
                // We need to decide whether to modify nums[i] or nums[i+1].
                // To keep the array non-decreasing, we prefer lowering nums[i].
                // This is possible if nums[i-1] <= nums[i+1] (or if i is 0).
                // Let's check the condition where we CAN'T lower nums[i].
                // This happens when i > 0 and nums[i-1] > nums[i+1].
                // In this case, we MUST raise nums[i+1].
                if (i > 0 && nums[i - 1] > nums[i + 1]) {
                    nums[i + 1] = nums[i];
                } 
                // Otherwise, we can lower nums[i]. For example, by setting nums[i] = nums[i+1].
                // This change doesn't need to be coded because the element at nums[i]
                // won't be a part of any future nums[k] > nums[k+1] checks where k >= i.
            }
        }
        return true;
    }
};

Non-decreasing Array in TypeScript

function checkPossibility(nums: number[]): boolean {
    let modifications = 0;
    for (let i = 0; i < nums.length - 1; i++) {
        if (nums[i] > nums[i + 1]) {
            // If we have already made a modification, we can't make another one.
            if (modifications === 1) {
                return false;
            }
            modifications++;

            // We have a violation at index i: nums[i] > nums[i+1].
            // We need to decide whether to modify nums[i] or nums[i+1].
            // To keep the array non-decreasing, we prefer lowering nums[i].
            // This is possible if nums[i-1] <= nums[i+1] (or if i is 0).
            // Let's check the condition where we CAN'T lower nums[i].
            // This happens when i > 0 and nums[i-1] > nums[i+1].
            // In this case, we MUST raise nums[i+1].
            if (i > 0 && nums[i - 1] > nums[i + 1]) {
                nums[i + 1] = nums[i];
            } 
            // Otherwise, we can lower nums[i]. For example, by setting nums[i] = nums[i+1].
            // This change doesn't need to be coded because the element at nums[i]
            // won't be a part of any future nums[k] > nums[k+1] checks where k >= i.
        }
    }
    return true;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.