Task:
A left rotation operation on an array of size n shifts each of the array’s elements 1 unit to the left. Given an integer,d, rotate the array that many steps left and return the result.
Function Description
Complete the rotateLeft function in the editor below.
rotateLeft has the following parameters:
- int d: the amount to rotate by
- int arr[n]: the array to rotate
Returns
- int[n]: the rotated array
Solution:
Javascript:
function rotateLeft(d, arr) {
// Write your code here
for(var i=d;i<arr.length;i++)
arr.unshift(arr.pop());
return arr;
}
Java:
public static List<Integer> rotateLeft(int d, List<Integer> arr) {
// Write your code here
for(int i=0;i<d;i++){
arr.add(arr.remove(0));
}
return arr;
}