Minimum Moves to Match Arrays

Quick Overview

Given two integer arrays of the same length, count the fewest single-step increments or decrements needed to make them equal at every index without reordering. It tests reasoning about independent per-index costs, proving a lower bound, and handling sums that overflow 32-bit integers.

Minimum Moves to Match Arrays

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given two integer arrays `nums1` and `nums2` of the same length. Find the minimum number of moves needed to make them identical. A move increments or decrements a single element of `nums1` by 1. Reordering elements is not allowed, so `nums1[i]` must end up equal to `nums2[i]` for every index `i`. Return the total minimum number of moves. ### Function Signature ```python def min_moves(nums1: list[int], nums2: list[int]) -> int: ``` ### Constraints - `1 <= len(nums1) <= 10^4` - `len(nums1) == len(nums2)` - `-10^9 <= nums1[i], nums2[i] <= 10^9` ### Examples ```text Input: nums1 = [1, 2, 3], nums2 = [3, 2, 1] Output: 4 ``` Index 0 needs |1 - 3| = 2 moves, index 1 needs |2 - 2| = 0 and index 2 needs |3 - 1| = 2, for a total of 4. ```text Input: nums1 = [1, 4, 7, 10], nums2 = [2, 3, 8, 5] Output: 8 ``` The per-index costs are 1, 1, 1 and 5, which add up to 8. ```text Input: nums1 = [-1000000000, 7, 1000000000], nums2 = [1000000000, -3, -1000000000] Output: 4000000010 ``` Index 0 and index 2 each need 2,000,000,000 moves and index 1 needs |7 - (-3)| = 10. The total is larger than a 32-bit signed integer can hold.

Overview: Given two integer arrays of the same length, count the fewest single-step increments or decrements needed to make them equal at every index without reordering. It tests reasoning about independent per-index costs, proving a lower bound, and handling sums that overflow 32-bit integers.

Salesforce logo
Salesforce
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given two integer arrays nums1 and nums2 of the same length. Find the minimum number of moves needed to make them identical.

A move increments or decrements a single element of nums1 by 1. Reordering elements is not allowed, so nums1[i] must end up equal to nums2[i] for every index i.

Return the total minimum number of moves.

Function Signature

def min_moves(nums1: list[int], nums2: list[int]) -> int:

Constraints

  • 1 <= len(nums1) <= 10^4
  • len(nums1) == len(nums2)
  • -10^9 <= nums1[i], nums2[i] <= 10^9

Examples

Input: nums1 = [1, 2, 3], nums2 = [3, 2, 1]
Output: 4

Index 0 needs |1 - 3| = 2 moves, index 1 needs |2 - 2| = 0 and index 2 needs |3 - 1| = 2, for a total of 4.

Input: nums1 = [1, 4, 7, 10], nums2 = [2, 3, 8, 5]
Output: 8

The per-index costs are 1, 1, 1 and 5, which add up to 8.

Input: nums1 = [-1000000000, 7, 1000000000], nums2 = [1000000000, -3, -1000000000]
Output: 4000000010

Index 0 and index 2 each need 2,000,000,000 moves and index 1 needs |7 - (-3)| = 10. The total is larger than a 32-bit signed integer can hold.