Skip to content

Instantly share code, notes, and snippets.

@thmain
Created January 15, 2021 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/4097d6384fbde6683fa3ecbc60ee8035 to your computer and use it in GitHub Desktop.
Save thmain/4097d6384fbde6683fa3ecbc60ee8035 to your computer and use it in GitHub Desktop.
public class NonDecreasingArray {
public static boolean check(int[] input) {
int countR = 0;
int len = input.length;
int prev = input[len-1];
for(int i=len-2;i>=0;i--){
if(prev<input[i]){
countR++;
}else
prev = input[i];
}
int countL = 0;
prev = input[0];
for(int i=1;i<len;i++){
if(prev>input[i]){
input[i] = prev+1;
countL++;
}else
prev = input[i];
}
int result = Math.min(countR, countL);
return result>1?false:true;
}
public static void main(String[] args) {
System.out.println(check(new int []{4, 5, 1, 7}));
System.out.println(check(new int []{10, 5, 2}));
System.out.println(check(new int []{1, 2, 1, 5}));
System.out.println(check(new int []{1, 1, 1, 1}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment