Rotate the array by N positions

Rotate the array by N position

 


import java.util.ArrayList;
import java.util.List;
/**
* @author Gaurav Gupta
*		
*/
public class RotateArray {
public static List<Integer> rotateArray(List<Integer> A, int B) {
List<Integer> ret = new ArrayList<Integer>();
for (int i = 0; i < A.size(); i++) {
ret.add(A.get((i + B)%A.size()));
}
return ret;
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
System.out.println(rotateArray(list,12));
}
}

 
Output:




No Comments Yet

Leave a Reply

Your email address will not be published.

Lorem ipsum dolor sit amet, consectetur a dipiscing elit. Vivamus leo ante,

FOLLOW US ON