Skip to content

Instantly share code, notes, and snippets.

@cdaz5
Created October 1, 2017 15:19
Show Gist options
  • Select an option

  • Save cdaz5/74fe82c3540a6cd04513009ab4d7c3e0 to your computer and use it in GitHub Desktop.

Select an option

Save cdaz5/74fe82c3540a6cd04513009ab4d7c3e0 to your computer and use it in GitHub Desktop.
function main() {
// provided by hackerrank.
var n_temp = readLine().split(' ');
var n = parseInt(n_temp[0]);
var k = parseInt(n_temp[1]);
a = readLine().split(' ');
a = a.map(Number);
// we loop through the array of numbers but rather than doing it relative to the length
// we set it to run while i is less than the number of left rotations we are being
// asked to do.
for (let i = 0; i < k; i++) {
// set a variable equal to the the first element in the array which we pull out by calling .shift().
first = a.shift()
// push that variable (which was the first element in the array) back onto the array where
// it is now that last element in the array.
a.push(first)
}
// join the array then log it, super straight forward.
console.log(a.join(' '))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment