Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am using javascript or c++ /* * Complete the 'minimumMoves' function below. * * The function is expected to return an INTEGER. * The

image text in transcribedimage text in transcribed

I am using javascript or c++

/* * Complete the 'minimumMoves' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER_ARRAY a * 2. INTEGER_ARRAY m */

js:

function minimumMoves(a, m) { // Write your code here

}

c++:

int minimumMoves(vector a, vector m) {

}

Andrea and Maria each have an array of integers. Andrea wants to change her array to match Maria's For each element of her array, she can increment or decrement one digit in one item in one move. How many moves will it take Andrea to match Maria's array. No reordering of the digits is allowed For example, consider two arrays: Andrea's [123, 543] and Maria's [321, 279]. For the first integer, Andrea can increment the 1 twice to achieve 3. The 2's are equal already. Finally, she decrements her 3 twice to equal 1. It took 4 moves to reach her goal. For the second integer, she decrements 5 three times, increments 4 three times and 3 six times. It took 12 moves to convert the second array element. In total, it took 16 moves to convert both values comprising the complete array. Function Description Complete the function minimumMoves in the editor below. The function must return the integer number of moves to convert Andrea's array to match Maria's. minimumMoves has the following parameter(s): ala[0O)....a. n-1]1: Andreas's array of integers mlm(0)1...mn-1]1: Maria's array of integers Constraints 1sns 105 1 s alil, mlils 10 The lengths of a and m are equal, la m| The elements alil and mlil have an equal number of digits. Input Format for Custom Testing Input from stdin will be processed as follows and passed to the function The first line contains an integer n, the size of the array a The next n lines each contain an element ali] where 0si 2234 2. Increment the second digit: 2234- 2334 3. Increment the third digit: 2334 2344 4. Increment the fourth digit: 2344 2345 For al1]ml1], we perform the following 6 moves 1. Decrement the first digit: 43213321 2. Decrement the second digit: 33213221 3. Decrement the third digit: 3221 3211 4. Increment the fourth digit three times: 3211- 32123213- 3214 We then return the minimum number of necessary moves to convert the entire array, which is 4 +6- 10<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What online recruitment methods are available?

Answered: 1 week ago

Question

=+j Enabling a productive global workforce.

Answered: 1 week ago