Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2552

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T06:28:07+00:00 2024-11-26T06:28:07+00:00

Day 15 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#453. Minimum Moves to Equal Array Elements(M/JS)

  • 61k

Intro: I am a former accountant turned software engineer graduated from coding bootcamp in January 2022. Algorithms and Data Structure is an unavoidable part of interviews for most of the tech companies now. And one of my friends told me that you need to solve a medium leetcode problem under 60 seconds in order to get into the top tech companies.So I thought I'd start learning how to do it while job searching.

Since I have no clue on how to solve any of the problems (even the easy ones), I thought there is no point for me to waste hours and can't get it figured out. Here is my approach:

  • Pick a leetcode problem randomly or Online Assessment from targeted companies.
  • Study 1-2 solutions from Youtube or LeetCode discussion section. One brute force solution, another one more optimal.
  • Write a blog post with detailed explanation and do a verbal walk through to help understand the solutions better.
  • Code out the solution in LeetCode without looking at the solutions
  • Combat the forgetting curve: Re-do the question for the next three days. And come back regularly to revisit the problem.

453. Minimum Moves to Equal Array Elements

It's a Math question…

Difficulty: Medium Language: JavaScript

Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.

In one move, you can increment n - 1 elements of the array by 1.

Example 1:

Input: nums = [1,2,3] Output: 3 Explanation: Only three moves are needed (remember each move increments two elements): [1,2,3]  =>  [2,3,3]  =>  [3,4,3]  =>  [4,4,4] 
Enter fullscreen mode Exit fullscreen mode

Example 2:

Input: nums = [1,1,1] Output: 0 
Enter fullscreen mode Exit fullscreen mode

Constraints:

  • n == nums.length
  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109
  • The answer is guaranteed to fit in a 32-bit integer.

Solution:
My first thought was to:

  • Get the difference between sum of all array element and largest number*n. That will give us the number of moves are needed to get all elements to equal to the largest number.
  • Because “In one move, you can increment n – 1 elements of the array by 1”; the number of moves needed need to be divisible by n-1. If not, increase the largest number by 1 until that condition is met.
  • For example, given array [1,2,3], the number of moves needed to get all element to equal to the largest number '3' is 3 (3*3 – (1+2+3)). But 3 is not divisible by n-1, 2 in this case. We increase largest number from 3 to 4. Now the moves needed for all elements to equal to largest number '4' is 6 (4*3 – (1+2+3)). 6 divided by 2 is 3. Hence, 3 moves is the answer for array [1,2,3].

User 'spacepumpkin' on LeetCode provided a much better idea:

  • if we think of it the other way around, incrementing n-1 elements other than nums[i] is effectively the same as decrementing nums[i] to make all elements equal
  • thus, the number of moves to increment n-1 elements is the same as the number of moves to decrement each element to get to the minimum
  • I like to think of each element as a tower of blocks — how many blocks do we have to remove so that all towers are at the minimum tower height?
ex. for [1, 2, 4], we have:       [x]       [x]    [x][x] [ ][ ][ ]  -- remove 4 blocks (x) --> [ ][ ][ ] 
Enter fullscreen mode Exit fullscreen mode

so, based on this way of thinking, the formula is:
number of blocks removed = (sum of all 'blocks') - (number of towers * minimum tower height)
(in our example, total # blocks = 7, number of towers = 3, & minimum tower height = 1)

Code:

function minMoves(nums) {   let sum = nums[0];   let min = nums[0];  //Initialize both 'sum' and 'min' variable as first number (note2) //in the array    for (let i = 1; i < nums.length; i++) {   //Loop (note 1) through 'nums' array and find the total # of //blocks and min tower height      if (nums[i] < min) min = nums[i];  //if an element if found to be smaller than current 'min', then //replace current value of 'min' to that smaller element found. //To find the min tower height.      sum += nums[i];  //add value to every element to 'sum' to get sum of all //element(total # of blocks).    }    return sum - (nums.length * min);  //# blocks removed = total # blocks - (# towers * min height) <-- //refer to explation above regarding removing blocks  }; 
Enter fullscreen mode Exit fullscreen mode

Solution Submission detail as of 2/26/2022
(Data below could vary since there are new tests/submissions daily)

  • Runtime: 72 ms
  • Memory Usage: 45.1 mb

References:
LeetCode Problem Link
LeetCode Discussion: spacepumpkin
Note 1: Loop and Iteration
Note 2: Access an array item by its index
Note 3: Addition assignment(+=)
Blog Cover Image Credit

algorithmsbeginnersjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.