Skip to content

Instantly share code, notes, and snippets.

@juliaamosova
Created September 27, 2017 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliaamosova/c2dea6512a1bea932b4fefcc5f4add3b to your computer and use it in GitHub Desktop.
Save juliaamosova/c2dea6512a1bea932b4fefcc5f4add3b to your computer and use it in GitHub Desktop.
Given an array of n integers and a number , d, perform d left rotations on the array. Then print the updated array as a single line of space-separated integers.
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function leftRotation(a, d) {
// Complete this function
var firstElem;
var lastElem;
for(i = 0; i < d; i++) {
firstElem = a.shift();
lastElem = a.push(firstElem);
}
return a;
}
function main() {
var n_temp = readLine().split(' ');
var n = parseInt(n_temp[0]);
var d = parseInt(n_temp[1]);
a = readLine().split(' ');
a = a.map(Number);
var result = leftRotation(a, d);
console.log(result.join(" "));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment