Rotation of an array using O(1) Space Complexity Java
PROGRAM TO ROTATE AN ARRAY USING REVERSAL ALGORITHM
import java.util.Scanner;
public class Main {
static void reverse(int arr[], int start, int end) {
while(start<end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// n = number of array elements
int n = sc.nextInt();
// k = number of positions by which array should be rotated
int k = sc.nextInt();
int[] arr = new int[n];
for(int j=0; j<n; j++)
arr[j] = sc.nextInt();
k %= n;
reverse(arr, 0, arr.length-1);
reverse(arr, 0, k-1);
reverse(arr, k, arr.length-1);
for(int j=0; j<n; j++)
System.out.print(arr[j] + " ");
}
}
OUTPUT
5 1
1 2 3 4 5
5 1 2 3 4
Comments
Post a Comment