Created
April 19, 2020 12:43
-
-
Save kirylrb/b55073cdc07639b065f58157b5448344 to your computer and use it in GitHub Desktop.
Rotation of the array in Ruby
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
# An array A consisting of N integers is given. | |
# Rotation of the array means that each element is shifted right by one index, | |
# and the last element of the array is moved to the first place. | |
# For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] | |
# (elements are shifted right by one index and 6 is moved to the first place). | |
def solution(a, k) | |
# write your code in Ruby 2.2 | |
k.times do | |
a = [a.last] + a.shift(a.size-1) | |
end | |
return a | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment