Day 4 of 60 Days of DSA — Arrays: Rotation, Equilibrium Index & Product Puzzle

Three powerful array problems that test your understanding of prefix sums, two-pointer logic, and modular thinking.

Intro Image

Welcome to Day 4 of the 60 Days of DSA Challenge!

By the end of this article, you’ll be confident about:

  • Efficiently rotating an array using the reversal algorithm
  • Finding the equilibrium index in a single pass
  • Solving the product array puzzle without using division

Let’s dive in

Problem 1: Array Rotation

Given an integer array A of size N and an integer B, you have to return the same array after rotating it B times towards the right.

Problem Constraints

1 <= N <= 105
1 <= A[i] <=109
1 <= B <= 109

Input Format

The first argument given is the integer array A.
The second argument given is the integer B.

Output Format

Return the array A after rotating it B times to the right

Example Input

Input 1:
A = [1, 2, 3, 4]
B = 2
Input 2:
A = [2, 5, 6]
B = 1

Example Output

Output 1:
[3, 4, 1, 2]
Output 2:
[6, 2, 5]

Example Explanation

Explanation 1:
Rotate towards the right 2 times — [1, 2, 3, 4] => [4, 1, 2, 3] => [3, 4, 1, 2]
Explanation 2:
Rotate towards the right 1 time — [2, 5, 6] => [6, 2, 5]

Solution

Intuition

When you rotate an array to the right by B positions:

  • The last B elements move to the front.
  • The remaining elements shift to the right.

But instead of performing one rotation at a time (which is costly),
we can use the reversal algorithm — a super-efficient trick for rotation.

Approach — The Reversal Algorithm

  1. Reverse the entire array.
  2. Reverse the first B elements.
  3. Reverse the remaining N - B elements.

Let’s visualise this for A = [1, 2, 3, 4, 5], B = 2:

Step 1: Reverse full array → [5, 4, 3, 2, 1]
Step 2: Reverse first B=2 elements → [4, 5, 3, 2, 1]
Step 3: Reverse remaining N-B=2 elements → [4, 5, 1, 2, 3]

Boom! ✅ Rotation done in O(N).

Code

// Helper functions
function swap(A, a, b) {
[A[a], A[b]] = [A[b], A[a]];
}

function reverse(A, start, end){
let mid = start + Math.floor((end-start)/2);
for(let i = start, j =end ; i<= mid; i++, j--){
swap(A, i ,j);
}
}
// Main function
function arrayRotation(A, B){
let n = A.length;
let rotateCount = B % n;
if (rotateCount == 0) {
return A;
}
// reverse complete array
reverse(A, 0, n-1)
// reverse both parts seperately
let part1 = rotateCount-1;
reverse(A, 0, part1);
reverse(A, part1+1, n-1);
return A;
}
arrayRotation([1,2,3,4,5], 2)

Complexity

  • Time: O(N)
  • Space: O(1)

🧩 Quick Quiz

If A = [10, 20, 30, 40, 50] and B = 3What’s the rotated array?
(Comment your answer before checking!)

Problem 2: Equilibrium Index of an Array

You are given an array A of integers of size N.
Your task is to find the equilibrium index of the given array
The equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes.
If there are no elements that are at lower indexes or at higher indexes, then the corresponding sum of elements is considered as 0.
Note:
Array indexing starts from 0.
If there is no equilibrium index then return -1.
If there are more than one equilibrium indexes then return the minimum index.

Problem Constraints

1 <= N <= 105
-105 <= A[i] <= 105

Input Format

First arugment is an array A .

Output Format

Return the equilibrium index of the given array. If no such index is found then return -1.

Example Input

Input 1:
A = [-7, 1, 5, 2, -4, 3, 0]
Input 2:
A = [1, 2, 3]

Example Output

Output 1:
3
Output 2:
-1

Example Explanation

Explanation 1:

i Sum of elements at lower indexes Sum of elements at higher indexes
Image of left sum and right sum for input 1
3 is an equilibrium index, because:
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]

Explanation 2:

i Sum of elements at lower indexes Sum of elements at higher indexes
Image of left sum and right sum for input 2
Thus, there is no such index.

Solution

Intuition

We can’t afford to calculate left and right sums for every index (that’s O(N²)).
So, let’s precompute prefix sums.

  1. Calculate prefix sums (leftSum[i]) and suffix sums (rightSum[i]).
  2. For every index i, check if leftSum[i-1] == rightSum[i+1].

Code

function findEquilibrium(A){
let leftSum = [], rightSum = [];
let n = A.length;
for(let i=0, j =n-1; i<n; i++, j--){
if(i == 0){
leftSum[i] = A[i];
rightSum[j] = A[j];
}else{
leftSum[i] = leftSum[i-1] + A[i];
rightSum[j] = rightSum[j+1] + A[j];
}
}
for(let i=0; i< n; i++){
if(
i == 0 && rightSum[i+1] == 0
|| i == n-1 && leftSum[i-1] == 0
|| leftSum[i-1] == rightSum[i+1]

){
return i;
}
}
return -1;
}

Complexity

  • Time: O(N)
  • Space: O(N)

🧩 Quick Quiz

What will be the equilibrium index of [1, -1, 1, -1, 1, -1, 1]?
Think before you run it 😎

Problem 3: Product Array Puzzle

Given an array of integers A, find and return the product array of the same size where the ith element of the product array will be equal to the product of all the elements divided by the ith element of the array.
Note: It is always possible to form the product array with integer (32 bit) values. Solve it without using the division operator.

Input Format

The only argument given is the integer array A.

Output Format

Return the product array.

Constraints

2 <= length of the array <= 1000
1 <= A[i] <= 10

For Example

Input 1:
A = [1, 2, 3, 4, 5]
Output 1:
[120, 60, 40, 30, 24]
Input 2:
A = [5, 1, 10, 1]
Output 2:
[10, 50, 5, 50]

Solution

Intuition

To solve this without division:

  1. Build a prefix product array — product of all elements before index i.
  2. Build a suffix product array — product of all elements after index i.
  3. Multiply prefix[i] * suffix[i] for each index.

Code


function productArray(A){
let product = 1;
let countOfZeroes = 0;
for(let i=0; i< A.length; i++){
if(A[i] == 0){
countOfZeroes++;
}else{
product *= A[i];
}
}
let opArray = [];
switch(countOfZeroes){
case 1: {
for(let i=0; i< A.length; i++){
if(A[i] == 0){
opArray.push(product);
}else{
opArray.push(0);
}
}
return opArray;
}
case 0: {
for(let i=0; i< A.length; i++){
opArray.push(product/A[i]);
}
return opArray;
}
default:{
return new Array({length: A.length}).fill(0);
}

}
}

Complexity

  • Time: O(N)
  • Space: O(N)

⚡ Bonus Tip

If you’re allowed to use division (and no zero in the array), you can just multiply all elements and divide by each A[i]. But this prefix-suffix approach works in all cases 👌

Summary

At Dev Simplified, We Value Your Feedback 📊

👉 Follow us to not miss any updates.

👉 Have any suggestions? Let us know in the comments!

👉 Subscribe for free and join our growing community!