Minimum Moves to Match Arrays
Learn this problemProblem 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):
int arr1[n]: array to modifyint 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 = 10Match 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
More IBM problems
- Parent Process NumberOA · Seen Jul 2026
- Request Retry CountOA · Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA · Seen Jul 2026
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Spam Text ClassificationOA · Seen Jul 2026
- Count Ideal NumbersOA · Seen Jun 2026