Problem · Array

Minimum Moves to Match Arrays

Learn this problem
EasyIBM logoIBMFULLTIMEOA
See IBM hiring insights

Problem statement

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

minimumMoves(arr1: int[], arr2: int[]) → int

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

Examples

Example 1

arr1 = [1234, 4321]arr2 = [2345, 3214]return = 10

Match 1234 with 2345. Each digit changes by 1, requiring 4 moves. Match 4321 with 3214. The digit differences are 1, 1, 1, and 3, requiring 6 moves. The total is 4 + 6 = 10.

Example 2

arr1 = [2468]arr2 = [8642]return = 16

🍇 Source note: Corrected on 2026-07-17. The source image uses [2468], not [1248].

Compare the digits of 2468 and 8642 in their fixed positions.

  • |2 - 8| = 6
  • |4 - 6| = 2
  • |6 - 4| = 2
  • |8 - 2| = 6

The minimum number of moves is 6 + 2 + 2 + 6 = 16.

Constraints

  • 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.
  • More IBM problems

    drafts saved locally
    public int minimumMoves(int[] arr1, int[] arr2) {
      // write your code here
    }
    
    arr1[1234, 4321]
    arr2[2345, 3214]
    expected10
    checking account