public static int findPeek(int[] arr) {
if (arr[0] < arr[arr.length - 1])
return -1;
int lo = 0;
int hi = arr.length - 1;
return findPeek(arr, lo, hi);
}
private static int findPeek(int[] arr, int lo, int hi) {
if (lo > hi)
return -1;
int mid = (lo + hi) / 2;
if (mid != hi && arr[mid] > arr[mid + 1])
return mid + 1;
if (arr[lo] > arr[mid]) {
return findPeek(arr, lo, mid);
} else {
return findPeek(arr, mid + 1, hi);
}
}
public static void rearrange(int[] arr) {
int peek = findPeek(arr);
if (peek == -1) {
return;
}
int[] aux = new int[arr.length];
for (int i = 0; i < aux.length; i++) {
aux[i] = arr[i];
}
int pos = 0;
for (int i = peek; i < aux.length; i++) {
arr[pos++] = aux[i];
}
for (int i = 0; i < peek; i++) {
arr[pos++] = aux[i];
}
}
Tuesday, June 9, 2015
Google : Find Peek and Rearrange the order
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment