Introduction

The Rotate Array problem involves shifting the elements of an array by k positions.

Given an array arr[] of size n and an integer k, the task is to rotate the array to the right by k steps.

In right rotation:

  • the last elements move to the beginning
  • remaining elements shift toward the right

This is an important array manipulation problem that helps in understanding array reversal, modular arithmetic, and in-place transformations.

Example:

Input: arr[] = [1, 2, 3, 4, 5, 6]k = 2
Output: [5, 6, 1, 2, 3, 4] Explanation:
After rotating by 2 positions:
5 and 6 move to the front

Input: arr[] = [10, 20, 30, 40, 50]
k = 3
Output: [30, 40, 50, 10, 20]
Explanation:
Last 3 elements shift to the beginning.
Remaining elements move to the right.

Constraints

   1 <= n <= 10^5
   0 <= k <= 10^5
-10^9 <= arr[i] <= 10^9

Approach 1 : Brute Force (Using Extra Array)

Explanation

The simplest way to rotate the array is:

  1. Create another array
  2. Place each element at its new rotated position
  3. Copy elements back if required

The new index after rotation is calculated using:

 newIndex = (i + k) % n

This approach is simple but requires extra space

Steps

  1.  Create a new array of size n.
  2. Traverse the original array.
  3. Calculate rotated index using modulo operation.
  4. Place elements at correct rotated positions.
  5. Return rotated array.
Dry Run
Input Array: [1, 2, 3, 4, 5, 6]k = 2
n = 6
Move 1 to index (0 + 2) % 6 = 2
Move 2 to index (1 + 2) % 6 = 3
Move 3 to index (2 + 2) % 6 = 4
Move 4 to index (3 + 2) % 6 = 5
Move 5 to index (4 + 2) % 6 = 0
Move 6 to index (5 + 2) % 6 = 1
Final Result:
[5, 6, 1, 2, 3, 4]

Brute Force Code

Complexity Analysis

Time Complexity: O(n)Explanation:
Every element is moved once.
Space Complexity: O(n)
Explanation:
An extra array is used for rotation.

Approach 2 : Optimized Solution (Array Reversal Technique)

Explanation

We can rotate the array in-place using the Reversal Algorithm.

For right rotation by k:

  1. Reverse entire array
  2. Reverse first k elements
  3. Reverse remaining elements

This avoids extra space usage.

Steps

  1. Calculate:
    • k=k%n.
  2. Reverse the entire array.
  3. Reverse first k elements.
  4. Reverse remaining n-k elements.
  5. Final array becomes rotated.

Dry Run

Input Array: [1, 2, 3, 4, 5, 6]k = 2
Reverse entire array:
[6, 5, 4, 3, 2, 1]
Reverse first 2 elements:
[5, 6, 4, 3, 2, 1]
Reverse remaining elements:
[5, 6, 1, 2, 3, 4]
Final Result:
[5, 6, 1, 2, 3, 4]

Optimized Code 

Complexity Analysis

Time Complexity: O(n)Explanation:
The array is reversed multiple times but each element is processed a constant number of times.
Space Complexity: O(1)
Explanation: Rotation is performed in-place without extra arrays.

Edge Cases

  1. k=0.
  2. k>n.
  3. Array contains one element
  4. Array contains duplicate elements
  5. Array size equals rotation count

Why This Problem is Important

This problem helps in understanding:

  1. Array reversal technique
  2. In-place manipulation
  3. Modular arithmetic
  4. Rotation logic
  5. Space optimization

It is one of the most important array manipulation interview problems.

Real-World Applications

Array rotation concepts are used in:

  1. Circular buffers
  2. Scheduling systems
  3. Data streaming
  4. Image rotation
  5. Cryptographic transformations

Common Mistakes

  1. Forgetting modulo operation
  2. Incorrect reversal order
  3. Wrong rotation direction
  4. Out-of-bound index calculations

Interview Tips

Interviewers often expect:

  1. In-place rotation solution
  2. O(n) time complexity
  3. Reversal algorithm explanation

Always explain why the reversal approach avoids extra space usage.

Related Questions

  1. Move Zeroes
  2. Reverse Array
  3. Rotate Linked List
  4. Cyclic Rotation
  5. Rearrange Array

Final Takeaway

The Rotate Array problem is a fundamental array manipulation problem that teaches in-place transformations, reversal techniques, and modular arithmetic. Understanding this problem builds a strong foundation for advanced array and cyclic traversal interview problems.