Skip to content

Instantly share code, notes, and snippets.

@gabhi
Created June 26, 2014 23:25
Show Gist options
  • Save gabhi/16903a8d31c0f7a815f9 to your computer and use it in GitHub Desktop.
Save gabhi/16903a8d31c0f7a815f9 to your computer and use it in GitHub Desktop.
Array rotate in place
public class ArrayRotate {
static void leftRotate(int arr[], int d, int n) {
rvereseArray(arr, 0, d - 1);
rvereseArray(arr, d, n - 1);
rvereseArray(arr, 0, n - 1);
}
static void rvereseArray(int arr[], int start, int end) {
int i;
int temp;
while (start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
static void printArray(int arr[], int size) {
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
leftRotate(arr, 2, 7);
printArray(arr, 7);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment