# Rotating an Array by K Steps in JavaScript

# This article explains my solution to Rotating an Array Question on Leetcode and the logic behind my solution.

Olamilekan Arimoro (TheCodeLord)

**Introduction**

Recently I started a journey of solving at least one Data Structures and Algorithm question daily on Leetcode in order to understand the concepts I am learning well and it has been very challenging and interesting since I started solving these questions.

This article will explain my solution to ** Rotating an Array Question** on Leetcode and the logic behind my solution. Try to follow it step by step and I am sure you will understand every bit of it.

Let's get right in ๐

**Question**

*Given an array, rotate the array to the right by k steps, where k is non-negative.*

**Example**

- Input: nums = [1,2,3,4,5,6,7], k = 3
- Output: [5,6,7,1,2,3,4]

**Explanation:**

- rotate 1 steps to the right: [7,1,2,3,4,5,6]
- rotate 2 steps to the right: [6,7,1,2,3,4,5]
- rotate 3 steps to the right: [5,6,7,1,2,3,4]

**Step by Step Solution**

Solving a Data Structures and Algorithm question requires you to think well and check your solution using different scenarios because your solution must pass all the test cases before it can be accepted.

We will solve this problem in 2 steps, which are:

- Doing Sanity Checks.
- Explaining the Logic behind our solution and writing the Code to back it up.

**Sanity Checks**

For the checks, we need to check the array (nums) to be sure that it is not an empty array and to see if the array is valid. If these conditions are met then the original array should be returned.

`// Sanity Checks`

if (!nums || !nums.length) {

return nums;

};

But if none of these conditions is met then we can proceed to carry out our array rotation to the right.

**Solution Logic**

We will consider two conditions while solving this, and the conditions are:

- When K is less than the array length.
- When K is greater than the array length.

**When K is less than the array length**

This is a normal condition and it is very easy to implement. In this solution we will use two JavaScript methods which are `splice`

and `unshift`

.

We will cut the number of k elements from the back of the array by doing `splice(length - k)`

with the splice method. That means if k is 3 then it cuts 3 elements from the back of the array.

So when we cut these elements we can now append it to the front of the array using the `unshift`

method but we also need to add spread operator to the front of the splice because splice creates another array and by adding spread operator to the front of splice it copies it into the main array and remove the new array created.

The code for the explanation above is shown below

`if (k < len) {`

nums.unshift(...nums.splice(length - k));

}

Now let's implement for the second condition, if k > array.length

**When K is greater than the array length**

Since k is greater than array length then we can't cut the number of k from the array because it won't be enough.

What we will do is to iterate through the array k number of times and keep appending the last item in the array to the front. so this will make the number of steps complete.

The code for the explanation is below:

`else {`

let i = 0;

while (i < k) {

nums.unshift(nums.splice(length - 1));

i++;

}

}

The Complete code that compiles the steps above is below:

`// Sanity Checks`

if (!nums || nums.length <= 1) {

return nums;

};

// Main Operation

len = nums.length;

if (k < len) {

nums.unshift(...nums.splice(len - k));

} else {

let i = 0;

while (i < k) {

nums.unshift(nums.splice(len - 1));

i++;

}

}

**The Runtime and Memory of my method**

This method I used has a Runtime: 104ms, faster than 91.47% of JavaScript online submissions for Rotate Array and Memory Usage: 49 MB, less than 38.14% of JavaScript online submissions for Rotate Array. This is not the best solution but I believe in consistency and improvement and I know that I will get better at it with constant practice๐.

**Conclusion**

I will like to say that keep practicing, the secret to getting better is doing it consistently and I have decided to do it consistently.

I hope you have learnt a lot from this article and your contribution is also highly welcomed because we get better by learning from others, you can reach out to me on Twitter ** here**. Watch out for more helpful contents on Data Structures and Algorithm from me.

Don't forget to Like, share and subscribe for more articles in this series called Solutions to Data Structures and Algorithm Questions on Leetcode.

Enjoy ๐

Upvote

Olamilekan Arimoro (TheCodeLord)

I am a result driven Frontend Developer and Technical Writer, I so much believe in writing about what I know and build because I will learn more and also teach more people about it. In my year of experience I have worked with about 3 teams and across different projects both company and personal projects. I am currently a Frontend Developer at CampHouse, I also have my blog on Hashnode where I write about Frontend related topics and general topics about Software Development

Related Articles