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] &lt;= nums[i + 1] holds for every i (0-based) such that (0 &lt;= i &lt;= n - 2).

&nbsp;
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.


&nbsp;
Constraints:


	n == nums.length
	1 &lt;= n &lt;= 104
	-105 &lt;= nums[i] &lt;= 105


class Solution:
    def checkPossibility(self, nums: List[int]) -> bool:
        unstable=0
        for i in range(1,len(nums)):
            if nums[i]<nums[i-1]:
                unstable+=1
                if i<2 or nums[i-2]<=nums[i]:
                    nums[i-1]=nums[i]
                else:
                    nums[i]=nums[i-1]
                
                if unstable>1:
                    return False
        return True
class Solution {
    public boolean checkPossibility(int[] nums) {
        int modified = 0, prev = nums[0], index = 0;
        for (; index < nums.length; ++index) {
            if (nums[index] < prev) {
                if (++modified > 1) return false;
                if (index - 2 >= 0 && nums[index - 2] > nums[index]) continue;
            }
            prev = nums[index];
        }
        return true;
    }
}
class Solution {
public:
bool checkPossibility(vector<int>& nums) {
    if(nums.size()<=2)
        return true;
    //creating a diff array(size=n-1) to store differences bw 2 consecutive ele.
    vector<int>diff(nums.size()-1);
    int neg=0;
    
    for(int i=0;i<nums.size()-1;i++)
    {
        diff[i]=nums[i+1]-nums[i];
        diff[i]<0?neg++:0; //no of negative differences
    }
    
    if(neg>1)
        return false;

    
    for(int i=0;i<diff.size();i++)
    {
        if(diff[i]<0 and i!=diff.size()-1)
        {
            if(i==0)
                // if(diff[i+1]>=0)
                continue; //if the first diff is neg, the next is obviosly>=0 as neg<=1, and this mountain can be resolved;
                // else
                    // return false;
            
            if((nums[i-1]<=nums[i+1]) || (diff[i+1])>=abs(diff[i]))
                continue;  //the major mountain cases, lemme know below if want explaination of these
            else 
                return false;
        }
    }   
    return true;
}
var checkPossibility = function(nums) {
    let changed = false
    for(let i=nums.length-1; i>0; i--) {
        if(nums[i-1] > nums[i]) {
            if(changed) return false;
            if(i === nums.length-1 || nums[i-1] < nums[i+1]) nums[i] = nums[i-1]
            else nums[i-1] = nums[i];
            changed = true
        }
    }
    return true;
};
Non-decreasing Array