Python Solution For HackerRank Problem: Arrays: Left Rotation
Solving a simple HackerRank problem called: Arrays: Left Rotation using Python.
Problem
A left rotation operation on an array shifts each of the array's elements 1 unit to the left. For example, if 2 left rotations are performed on array [1, 2, 3, 4, 5] , then the array would become [3, 4, 5, 1, 2].
Given an array a of n integers and a number, d , perform d left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.
See the full description of the problem here : Arrays: Left Rotation
Solution
This is the logic. If a = 5, d = 4 1 2 3 4 5 Here alist[d:] = 5 alist[:d] = 1 2 3 4 so alist[d:]+alist[:d] = 5 1 2 3 4
Sample Input
5 4 1 2 3 4 5
Sample Output
5 1 2 3 4
Let me know if you got any better solution.
Comments
Leave a comment
You are not LoggedIn but you can comment as an anonymous user which requires manual approval. For better experience please Login .