LeetCode: Minimum Operations To Make Array Equal Solution
1/**2 * @param {number} n3 * @return {number}4 */5// O(n^2)6var minOperations = function (n) {7 const med = 2 * Math.floor(n / 2) + (n % 2 === 0 ? 0 : 1)8 return (9 Array.from({ length: n }, (_, i) => 2 * i + 1)10 .map(el => Math.abs(med - el))11 .reduce((acc, el) => acc + el, 0) / 212 )13}1415// O(1)16var minOperations = function (n) {17 return Math.floor(n / 2) * Math.ceil(n / 2)18}
Comments
Loading comments...
Tags
leetcode
math
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.