Arrays: Left Rotation HackerRank Solution,Looking for Array Left Rotation solution for Hackerrank problem? Get solution with source code and detailed explainer video
Here we have to perform 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]. Note that the lowest index item moves to the highest index in a rotation. This is called a circular array.Go to problem statement
Explanation Video:
Youtube Channel partner: CodingCart
Mentor: Satyendra Jaiswal
Langauge: Python
Source Code: for rotateLeft
function
def rotateLeft(d, a):
n = len(arr)
a=a[(n+d)%n:]+a[0:(n+d)%n]
return a
coming soon…
coming soon…
Full Source Code: Arrays: Left Rotation HackerRank Solution
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'rotateLeft' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. INTEGER d
# 2. INTEGER_ARRAY arr
#
def rotateLeft(d, a):
n = len(arr)
a=a[(n+d)%n:]+a[0:(n+d)%n]
return a
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
d = int(first_multiple_input[1])
arr = list(map(int, input().rstrip().split()))
result = rotateLeft(d, arr)
fptr.write(' '.join(map(str, result)))
fptr.write('\n')
fptr.close()
coming soon…
coming soon…
Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our Youtube Channel.
This post is in partner with CodingCart.