FastPrepFastPrep
Problem Brief

Minimum Moves to Match Arrays

FULLTIMEOA

There are two arrays of integers, arr1 and arr2. One move is defined as an increment or decrement of one element in an array. Determine the minimum number of moves to match arr1 with arr2. No reordering of the digits is allowed.

Function Description

Complete the function minimumMoves in the editor below.

minimumMoves has the following parameter(s):

  1. int arr1[n]: array to modify
  2. int arr2[n]: array to match

Returns

int: the minimum number of moves to match arr1 with arr2

1Example 1

Input
arr1 = [1234, 4321], arr2 = [2345, 3214]
Output
10
Explanation

- Match arr1[0]=1234 with arr2[0]=2345.

  • Increment 1 once to get 2 (1 move)
  • Increment 2 once to get 3 (1 move)
  • Increment 3 once to get 4 (1 move)
  • Increment 4 once to get 5 (1 move).
4 moves are needed to match 1234 with 2345. - Match arr1[1]=4321 with arr2[1]=3214.
  • Decrement 4 once to get 3 (1 move)
  • Decrement 3 once to get 2 (1 move)
  • Decrement 2 once to get 1 (1 move)
  • Increment 1 three times to get 4 (3 moves)
6 moves are needed to match 4321 with 3214. - 6+4=10 total moves are needed to match the arrays arr1 and arr2.

2Example 2

Input
arr1 = [1248], arr2 = [8642]
Output
16
Explanation

- Match arr1[0]=1248 with arr2[0]=8642.

  • Decrement 1 seven times to get 8 (7 moves)
  • Increment 2 four times to get 6 (4 moves)
  • Decrement 4 two times to get 2 (2 moves)
  • Increment 8 two times to get 4 (2 moves)
15 moves are needed to match 1248 with 8642. - 15 total moves are needed to match the arrays arr1 and arr2.

Constraints

Limits and guarantees your solution can rely on.

  • 1 ≤ n ≤ 105
  • 1 ≤ arr1[i], arr2[i] ≤ 109
  • The lengths of arr1 and arr2 are equal, |arr1| = |arr2|.
  • The elements arr1[i] and arr2[i] have an equal number of digits.
  • public int minimumMoves(int[] arr1, int[] arr2) {
      // write your code here
    }
    
    Input

    arr1

    [1234, 4321]

    arr2

    [2345, 3214]

    Output

    10

    Sign in to submit your solution.