Hackerank: Interview Prep Kit: Array: Left Rotation.

Samuel Ezedi
2 min readJan 17, 2021

--

Hello friends, we are going to be solving a problem I found on Hackerank, please stay focused as I share the problem and provide an answer. I will be providing answers majorly in Javascript and Dart.

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. Note that the lowest index item moves to the highest index in a rotation. This is called a circular array.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.

Function Description

Complete the function rotLeft in the editor below.rotLeft has the following parameter(s):* int a[n]: the array to rotate* int d: the number of rotations

Return

* int a'[n]: the rotated array

Input Format

The first line contains two space-separated integers n and d, the size of a and the number of left rotations.
The second line contains n space-separated integers, each an a[i].

Sample Input

5 4
1 2 3 4 5

Sample Output

5 1 2 3 4

Explanation

When we perform d = 4 left rotations, the array undergoes the following sequence of changes:[1,2,3,4,5] -> [2,3,4,51] -> [3,4,5,1,2] -> [4,5,1,2,3] -> [5,1,2,3,4]

Solution in JS

Solution in Dart

Thanks for your time. Please take time to review the problem before looking at the solution. Remember this is for educational purposes and nothing more. Also, you can follow me here on Linkedin and, on Twitter.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Samuel Ezedi
Samuel Ezedi

No responses yet

Write a response