Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding minimum steps to make array elements equal in JavaScript
We are required to write a JavaScript function that takes in a number, num as the only argument. The function should first construct an array of n elements based on the following rule:
arr[i] = (2 * i) + 1;
Therefore, if the input number is 5, then the array should be:
const arr = [1, 3, 5, 7, 9];
Our function is supposed to calculate and return the minimum number of steps it should take so that all the elements of the array become equal.
Understanding the Problem
Let us now define one step:
One valid step consists of choosing any two numbers from the array (distinct numbers) and adding 1 to the first and subtracting 1 from the second.
The goal is to make all elements equal to the target value (which is the input number). For the array [1, 3, 5, 7, 9] with target 5, we need to transfer values from elements greater than 5 to elements less than 5.
Solution Approach
The algorithm calculates how many units need to be added to elements below the target. Elements above the target will donate their excess to reach equilibrium.
const num = 5;
const minimumOperations = (num = 1) => {
if (num === 1) {
return 0;
}
let arr = new Array(num);
let i = 0;
let res = 0;
while (i < num) {
arr[i] = (2 * i) + 1;
if (arr[i] < num) {
res += num - arr[i];
}
i++;
}
return res;
};
console.log(minimumOperations(num));
6
Step-by-Step Breakdown
Let's trace through the example with num = 5:
const num = 5;
// Step 1: Build the array
let arr = [];
for (let i = 0; i < num; i++) {
arr[i] = (2 * i) + 1;
}
console.log("Array:", arr);
// Step 2: Calculate deficit for each element
let totalSteps = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] < num) {
let deficit = num - arr[i];
console.log(`Element ${arr[i]} needs ${deficit} steps`);
totalSteps += deficit;
}
}
console.log("Total minimum steps:", totalSteps);
Array: [ 1, 3, 5, 7, 9 ] Element 1 needs 4 steps Element 3 needs 2 steps Total minimum steps: 6
How It Works
The solution works because:
- Elements below the target need to receive units
- Elements above the target can donate their excess units
- Each step transfers exactly 1 unit from donor to receiver
- The minimum steps equals the total deficit of below-target elements
Alternative Implementation
const minimumStepsOptimized = (n) => {
if (n === 1) return 0;
let steps = 0;
for (let i = 0; i < n; i++) {
let value = (2 * i) + 1;
if (value < n) {
steps += n - value;
}
}
return steps;
};
console.log(minimumStepsOptimized(5)); // 6
console.log(minimumStepsOptimized(3)); // 2
console.log(minimumStepsOptimized(1)); // 0
6 2 0
Conclusion
The minimum steps to equalize the array equals the sum of deficits for all elements below the target value. This approach efficiently calculates the result without actually performing the transfer operations.
