Skip to content

Instantly share code, notes, and snippets.

@micheleorsi
Last active August 29, 2015 14:23
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 micheleorsi/801e68a73ceb6cd0d4ef to your computer and use it in GitHub Desktop.
Save micheleorsi/801e68a73ceb6cd0d4ef to your computer and use it in GitHub Desktop.
3rd exercise
public boolean solution(int[] A) {
if(isArrayInNonDecreasingOrder(A)) {
return true;
}
for(int i=0; i<A.length; i++) {
for(int j=0; j < A.length; j++) {
if(i!=j) {
int[] testArray = A.clone();
int tempValue = testArray[i];
testArray[i]=testArray[j];
testArray[j] = tempValue;
if(isArrayInNonDecreasingOrder(testArray)) {
return true;
}
}
}
}
return false;
}
public boolean isArrayInNonDecreasingOrder(int[] A) {
for (int i=1; i<A.length; i++) {
// non decreasing
if(A[i]<A[i-1]) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment