Created
January 15, 2021 05:21
-
-
Save thmain/4097d6384fbde6683fa3ecbc60ee8035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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