Rotate array #java

preview_player
Показать описание
LIFE CODING WITH JAVA

Rotate array
Send Feedback
You have been given a random integer array/list(ARR) of size N. Write a function that rotates the given array/list by D elements(towards the left).
Note:
Change in the input array/list itself. You don't need to return or print the elements.
Sample Input 1:
1
7
1 2 3 4 5 6 7
2
Sample Output 1:
3 4 5 6 7 1 2
Sample Input 2:
2
7
1 2 3 4 5 6 7
0
4
1 2 3 4
2
Sample Output 2:
1 2 3 4 5 6 7
3 4 1 2
I try to solve very easy concept So please watch this video .
All java code with easy trick.
By "LIFE CODING WITH JAVA". all program free no any cost So please like ,subscribe, share and comment.
If you any doubt related to any video then comment me.
As soon as I will help you .
my contact number: 6206472735
Рекомендации по теме
Комментарии
Автор

public class Solution {
public static int[] rotateArray(int []a, int x, String dir) {
// Write your code here.

int n =a.length;
int[] rotated = new int[n];
int er = (dir.equals("LEFT"))? (x%n) : (n - (x % n));
for(int i = 0; i<n;i++){
rotated[i]=a[(i+er)%n];
}
return rotated;
}
}

indrajeetyadav