# Documented Clean Java Solution

• ``````/**
Constraint
array can be empty
n > 1 && n < 10,000
arr[n] any integer

Test
[] -> true
[1,2] -> true
[2, 1, 3] -> true
[2, 1, 3, 2] -> false;
[3, 4, 2, 3] -> false;
[2, 3, 3, 2, 4] -> true

Ideas
if decrease happens more than once, it's undoable
if only one decrease find [a,b,c,d] where b > c, we have two choices
- decrease b, a must be smaller than c
- increase c, d must be greater than b
in case b is at the beginning or d is at the end, we don't have to make
extra test on the neighbors

*/
class Solution {
public boolean checkPossibility(int[] nums) {
if(nums.length < 3) return true;
boolean modified = false;
for(int i = 1; i < nums.length; i++) {
if(nums[i] >= nums[i-1]) continue;

if(modified) return false;

if(shouldCheckNeighbor(i, nums.length) &&
isRefusedByBothNeighbors(nums, i)) return false;

modified = true;
}
return true;
}

private boolean shouldCheckNeighbor(int i, int length) {
return i > 1 && i < length - 1;
}

private boolean isRefusedByBothNeighbors(int[] nums, int i) {
return nums[i] < nums[i - 2] && nums[i-1] > nums[i+1];
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.