Skip to content

Instantly share code, notes, and snippets.

@nicky1525
Last active July 21, 2018 19:22
Show Gist options
  • Save nicky1525/7d4503facf67252206607671d8249a93 to your computer and use it in GitHub Desktop.
Save nicky1525/7d4503facf67252206607671d8249a93 to your computer and use it in GitHub Desktop.
A left rotation operation on an array shifts each of the array's elements unit to the left. Given an array of integers and a number, perform left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.
func rotLeft(a: [Int], d: Int) -> [Int] {
var array: [Int] = []
for i in 0 ..< a.count {
let index = (i + d) % a.count
array.append(a[index])
}
return array
}
rotLeft(a: [1,2,3,4,5], d: 4) // output [5, 1, 2, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment