HackerRank Left Rotation

Solution for the HackerRank Left Rotation problem.

You can find the problem details on HackerRank.

You are provided with a template in your chosen programming language; in this example, we use Python 3.

The problem requires returning the array with elements shifted to the left by d positions. Note that while you will receive the length of the array as input, it is not explicitly needed to solve the problem.

Here is the Python code that solves the problem:

def rotateLeft(d, arr):
    return arr[d:] + arr[:d]

Explanation:

  • arr[d:] slices the list from the d-th element to the end.
  • arr[:d] slices the list from the beginning to the d-th element.
  • By concatenating these two slices, we achieve the left rotation of the array.
← Back to posts

We use cookies to improve your experience and analyze our traffic. By using our site, you consent to our use of cookies. You can manage your preferences below: