Explained and Solved: Rotate Array: DSA Problem

Simran BansalSimran Bansal
2 min read

Problem

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

Example 1:

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]

Example 2:

Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Explanation: 
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Constraints:

  • 1 <= nums.length <= 10<sup>5</sup>

  • -2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1

  • 0 <= k <= 10<sup>5</sup>

Solution

Intuition

If I have [1,2,3,4,5,6,7] and k=3 then I need to shift 3 elements cyclically.

With that, my final result will be [5,6,7,1,2,3,4]

Initially, I was solving it by traditional approach like single shift, which took me O(kN) time.

To think differently, I have tried to understand the approach in a bottom-up approach: solution to the problem.

If I see [5,6,7] that is the k partition, [1,2,3,4] that is shifted element partition, if I reverse [1,2,3,4] I will get [4,3,2,1].

If I look whole combined array at this step, then it will be [7,6,5,4,3,2,1] which is, yes, you are right, the reversal of the original array.

So here I decode it like this:

  • Go from [1,2,3,4,5,6,7] to [7,6,5,4,3,2,1]: Reverse the whole array.

  • Now I need to reverse [7,6,5] which is the last k element of the array, it will seem as if we rotate them when this subpartition has been swapped. So we will have: [5,6,7,4,3,2,1]

  • Now look carefully you will observe [5,6,7] are at their place. You only make [4,3,2,1] right.

  • Again you are correct, reverse this sub-partition: [4,3,2,1] -> [1,2,3,4]

  • Voila, we have [5,6,7,1,2,3,4], our exact answer.

Approach

  1. Reverse the whole array.

  2. Reverse up to k.

  3. Reverse from k+1 to the last index.

Complexity

  • Time complexity: O(N)

  • Space complexity: O(1)

Code

Java

class Solution {
    public void reverse(int[] nums, int start, int end)
    {
        while(start<end)
        {
          nums[end] = nums[start]+nums[end]-(nums[start]=nums[end]);
          start++;
          end--;
        }
    }
    public void rotate(int[] nums, int k) {
        k=k%nums.length;
        reverse(nums, 0, nums.length-1);
        reverse(nums, 0, k-1);
        reverse(nums, k, nums.length-1);
    }
}

Python

class Solution:

    def reverse(self, nums: List[int], start: int, end: int) -> None:
      while start<end:
        nums[start], nums[end] = nums[end], nums[start]
        start+=1
        end-=1

    def rotate(self, nums: List[int], k: int) -> None:
      k=k%len(nums)
      self.reverse(nums, 0, len(nums)-1)
      self.reverse(nums, 0, k-1)
      self.reverse(nums, k, len(nums)-1)

I have also posted the same solution on Leetcode: https://leetcode.com/problems/rotate-array/solutions/4376260/fully-explained-readable-and-100-efficient-java-solution/

0
Subscribe to my newsletter

Read articles from Simran Bansal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Simran Bansal
Simran Bansal

Hello, I'm Simran Bansal, a skilled graduate software engineer with expertise in C, Java, JavaScript, PHP, Python, SQL, data structures, functional programming, JSON, regular expressions, HTML, CSS, Bootstrap, React.js, Git, Jenkins, Selenium, Docker, AWS, Digital Ocean Droplets, Linode, Cloudflare Workers, Domain DNS, Nginx, Apache Hive, LaTeX, Eclipse, VS Code, Cucumber, SSH, Linux Administration, Kubernetes, and more. I am passionate about creating innovative software solutions that meet business requirements and exceed user expectations ๐Ÿš€. Additionally, I have experience with tools and technologies such as Audacity, Affinity Designer, Affinity Photo, and Photoshop CS6 ๐Ÿ’ป. My skills extend beyond technical expertise as I am also a skilled event coordinator, graphic designer, and project planner ๐ŸŽจ. I enjoy working with teams to plan and execute projects and believe in the power of strategic planning to achieve success ๐ŸŽฏ. I am an enthusiastic teacher and enjoy sharing my knowledge and experiences with others ๐Ÿ’ก. However, my weakness is handwriting, and I don't particularly like long lectures ๐Ÿ˜…. Outside of work, my hobbies are badminton, dancing, drawing, singing, and writing ๐Ÿธ. I am an avid consumer of anime, Marvel movies, and non-fiction, especially in the fields of technology, psychology, and computer programming ๐Ÿ“š. Throughout my career, I have developed small-scale software applications and small and medium-sized web applications, always focusing on teamwork and collaboration ๐Ÿค. I am excited about the opportunities ahead and always eager to learn and grow in my field ๐Ÿ˜Š. Currently at Nokia, I'm eager to collaborate and contribute to exciting projects. Feel free to connect with me at queries@sibansal.dev to explore opportunities or share insights.