# Java solution, 7 liner.

• ``````class Solution {
public boolean checkPossibility(int[] nums) {
int n = nums.length, count = 0;

for (int i = 0; i + 1 < n; i++) {
if (nums[i] > nums[i + 1]) {
count++;
if (i > 0 && nums[i + 1] < nums[i - 1]) nums[i + 1] = nums[i];
else nums[i] = nums[i + 1];
}
}

return count <= 1;
}
}
``````

• This post is deleted!

• I think it would still be correct without `else nums[i] = nums[i + 1];`

• @BatCoder I think `if` condition checks `i > 0` first, if `i == 0` then the rest part will not be checked

• @Allegro-Leon-Li Yes, got it. Sorry, my bad.

• @shawngao Could you kindly explain the logic?

• @Allegro-Leon-Li You are right. It can be done without that `else` part as well. The solution gets accepted without it.

• This post is deleted!

• This post is deleted!

• This post is deleted!

• can someone explain me this logic???

• @Allegro-Leon-Li

agree,
and it can be even faster:

public boolean checkPossibility(int[] nums) {
int n = nums.length, count = 0;

``````    for (int i = 0; i + 1 < n; i++) {
if (nums[i] > nums[i + 1]) {
count++;
if(count == 2)
{
return false;
}
if (i > 0 && nums[i + 1] < nums[i - 1]) nums[i + 1] = nums[i];
//else nums[i] = nums[i + 1];
}
}

return count <= 1;
}``````

• The solution without modifying the input array based on your post

``````class Solution {
public boolean checkPossibility(int[] nums) {
if (nums.length == 0) {
return true;
}
for (int i = 0, cur = nums[0], count = 0; i < nums.length - 1; i++) {
if (cur > nums[i + 1]) {
if (count == 1) {
return false;
}
if (!(i > 0 && nums[i - 1] > nums[i + 1])) {
cur = nums[i + 1];
}
count++;
} else {
cur = nums[i + 1];
}
}
return true;
}
}
``````

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