Created
September 27, 2017 01:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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