Minimum Moves to Match Arrays
Updated
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.
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
int: the minimum number of moves to match arr1 with arr2
Function
minimumMoves(arr1: int[], arr2: int[]) → intExamples
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 = 16Compare 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 interview questions
- Missing IntervalsOA · Seen Nov 2026
- Count Peaks and ValleysOA · Seen Nov 2026
- Last Object Exit TimeOA · Seen Nov 2026
- Count Strictly Increasing Contiguous WindowsOA · Seen Nov 2026
- Total Each Letter's Count in an Encoded StringOA · Seen Nov 2026
- Count Servers With No Requests in a Time WindowOA · Seen Nov 2026
- Alternate Odd and Even Numbers with Two GoroutinesPHONE SCREEN · Seen Nov 2026
- Longest Substring Without Repeating CharactersPHONE SCREEN · Seen Nov 2026
- Maximum Easy Tasks After RearrangingOA · Seen Nov 2026
- Longest Palindromic SubstringPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Total Cost Against the Lowest Earlier PricePayPal · OAPayPal interview questions
- Count Valid Square PairsAuxoAI · OA
- Picked Player Who LostSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Server InvestmentRippling · OARippling interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Next Pascal Row CoefficientIntuit · OAIntuit interview questions
- Unique Integers That Sum to ZeroNVIDIA · OANVIDIA interview questions
- Count Multiples of Three with Two SevensHudson River Trading · OAHudson River Trading interview questions
- Diagonal Bounce TraversalAutodesk · OAAutodesk interview questions
- Maximum Student-Teacher DistanceChubb · OAChubb interview questions