LeetCode 665: Non-decreasing Array (C) I'm posting a solution for LeetCode's &quot;Non-decreasing Array&quot;. If you'd like to review, please do. Thank you!
Problem
Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.
We define an array is non-decreasing if nums[i] &lt;= nums[i + 1] holds for every i (0-based) such that (0 &lt;= i &lt;= 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 can't get a non-decreasing array by modify at most one element.

Constraints:

1 &lt;= n &lt;= 10 ^ 4
-10 ^ 5 &lt;= nums[i] &lt;= 10 ^ 5

Code
// Since the relevant headers are already included on the LeetCode platform,
// the headers can be removed;
#include &lt;stdio.h&gt;
#include &lt;stdbool.h&gt;

static const bool checkPossibility(
    int *nums,
    const int nums_size
) {

    if (nums_size &lt; 3) {
        return true;
    }

    int max_changes = 0;

    for (int index = 1; index &lt; nums_size - 1; ++index) {
        if (!(nums[index] &gt;= nums[index - 1] &amp;&amp; nums[index + 1] &gt;= nums[index])) {
            if (nums[index + 1] &gt;= nums[index - 1]) {
                ++max_changes;
                nums[index] = nums[index - 1];

            } else {
                if (nums[index] &lt; nums[index - 1] &amp;&amp; nums[index + 1] &lt; nums[index]) {
                    return false;

                } else if (nums[index] &lt;= nums[index + 1]) {
                    nums[index - 1] = nums[index];

                    if (!(index - 1) || nums[index - 2] &lt;= nums[index - 1]) {
                        ++max_changes;

                    } else {
                        return false;
                    }

                } else {
                    nums[index + 1] = nums[index];
                    ++max_changes;
                }
            }
        }

    }


    return max_changes &lt; 2;
}

int main() {

    static const int nums_size = 3;
    int nums_array[nums_size] = {4, 2, 1};
    int (*nums)[nums_size] = &amp;nums_array;

    fputs(checkPossibility(*nums, nums_size) ? &quot;true&quot; : &quot;false&quot;, stdout);

    return 0;
}
The code mutates an array. It is not good.
The code does too much. You may return false as soon as max_changes reaches 2 (no need to examine the rest).
More functions please. It is very hard to follow the complicated decision making. Consider
int find_first_violation(int * nums, int size)
{
    int i = 0;
    for (; i &lt; size; i++) {
        if (nums[i] &lt; nums[i-1]) {
            break;
        }
    }
    return i;
}

Then the business logic would be:
    int violation = find_first_violation(nums, size);

    if (violation == size) {
        // array is already non-decreasing
        return true;
    }
    if (violation == size - 1) {
        // easily fixable: increase nums[size - 1]
        return true;
    }

    // Now fix the violation
    // violation == 1 is fixable by decreasing nums[0]. No action needed.
    // Otherwise, we only care about the case where nums[violation] is too
    // small - less than two preceding numbers. It is only fixable by
    // increasing it, effectively setting it equal to nums[violation - 1].

    if ((violation &gt; 1) &amp;&amp; (nums[violation] &lt; nums[violation - 2])) {
        nums[violation] = nums[violation - 1];
    }

    // Finally, the core argument to have more functions: there
    // must be no more violations.

    return find_first_violation(nums + violation, size - violation) == size - violation;

Of course the first two conditions can be combined into violation &gt;= size - 1. Of course increasing of nums[violation] can be virtual, without mutating the array (if nums[violation - 1] &gt; nums[violation + 1] we may immediately return false;).