Last active
July 21, 2018 19:22
-
-
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.
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
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