ARRAY LEFT ROTATION
ARRAY LEFT ROTATION
Sample Input
5 4
1 2 3 4 5
Sample Output
5 1 2 3 4
SOLUTION:
package Arrays;
import java.util.*;
public class ArrayRotation {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
// Complete the rotLeft function below.
int n1=sc.nextInt();
int d=sc.nextInt();
int a[]=new int[n1];
for(int i=0;i<n1;i++)
{
a[i]=sc.nextInt();
}
int length = a.length;
int j = 0;
int[] temp = new int[length];
for(int i = d; i < length; i++, j++) {
temp[j] = a[i];
}
if(j < length) {
for(int i = 0; i < d; i++, j++) {
temp[j] = a[i];
}
}
for(int i=0;i<n1;i++)
{
System.out.print(temp[i]+" ");
}
}
}
// Sample Input
// 5 4
// 1 2 3 4 5=> 1=> 2 3 4 5 1 ,2=> 3 4 5 1 2, 3=>4 5 1 2 3, 4=> 5 1 2 3 4
// Sample Output
// 5 1 2 3 4
Comments
Post a Comment