
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
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.
Therefore, for the above array, the output should look like −
const output = 6;
Example
The code for this will be −
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));
Output
And the output in the console will be −
6
- Related Articles
- Minimum operation to make all elements equal in array in C++
- Minimum operations required to make all the array elements equal in C++
- Minimum steps to make all the elements of the array divisible by 4 in C++
- Minimum Moves to Equal Array Elements in C++
- Minimum Moves to Equal Array Elements II in Python
- Minimum number of moves to make all elements equal using C++.
- Finding upper elements in array in JavaScript
- Finding minimum time difference in an array in JavaScript
- Program to find minimum operations to make array equal using Python
- Minimum Insertion Steps to Make a String Palindrome in C++
- Minimum Swaps to Make Strings Equal in C++
- Minimum delete operations to make all elements of array same in C++.
- Number of operations required to make all array elements Equal in Python
- Minimum operations of given type to make all elements of a matrix equal in C++
- Finding array intersection and including repeating elements in JavaScript

Advertisements