Minimum Operations to Make Array Equal to Target - Problem

You are given two positive integer arrays nums and target of the same length. Your goal is to transform the nums array to exactly match the target array using the minimum number of operations.

In each operation, you can:

  • Select any contiguous subarray of nums
  • Either increment all elements in that subarray by 1, or decrement all elements in that subarray by 1

The key insight is that you can choose different subarrays in each operation, and the subarrays can overlap or be completely separate. You want to find the minimum total number of operations needed to make nums[i] == target[i] for all indices.

Example: If nums = [3,5,1,2] and target = [4,6,2,4], you need to increase the first element by 1, second by 1, third by 1, and fourth by 2. The challenge is to do this efficiently by choosing optimal subarrays.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [3,5,1,2], target = [4,6,2,4]
โ€บ Output: 6
๐Ÿ’ก Note: Differences are [1,1,1,2]. We need 1 positive operation for positions 0-2, then 1 additional operation for position 3. Total: 1 + 1 = 2 operations? Actually, we need to carefully track: pos 0 needs +1, pos 1 needs +1, pos 2 needs +1, pos 3 needs +2. Using greedy: start 1 operation at pos 0, continue to pos 1,2, then start 1 more at pos 3. Total = 1 + 1 = 2. Wait, let me recalculate: we need 6 total operations.
example_2.py โ€” Mixed Operations
$ Input: nums = [1,3,2], target = [2,1,4]
โ€บ Output: 4
๐Ÿ’ก Note: Differences are [1,-2,2]. Need 1 positive op for pos 0, then 2 negative ops for pos 1, then 2 positive ops for pos 2. Total: 1 + 2 + 2 = 5 operations.
example_3.py โ€” Already Equal
$ Input: nums = [1,2,3], target = [1,2,3]
โ€บ Output: 0
๐Ÿ’ก Note: Arrays are already equal, so no operations needed.

Visualization

Tap to expand
๐Ÿ—๏ธ Smart Construction StrategyCurrent Heights:3512Target Heights:4624Adjustments Needed:+1+1+1+2๐Ÿ—๏ธ Crane Operations StrategyOperation 1: Raise buildings 0,1,2 (+1)Op 2: Raise bldg 3 (+1)Total: 2 crane operations (but 6 total adjustments)๐Ÿ’ก Key: Group adjacent buildings needing same adjustment type!
Understanding the Visualization
1
Survey Buildings
Calculate how much each building needs to be adjusted: diff[i] = target[i] - current[i]
2
Plan Operations
Use a smart strategy: when multiple adjacent buildings need the same type of adjustment, do them together
3
Execute Efficiently
Only start new crane operations when you need MORE operations than currently running
4
Natural Completion
Operations automatically end when buildings no longer need that level of adjustment
Key Takeaway
๐ŸŽฏ Key Insight: The optimal strategy is to think in terms of 'operation levels' rather than individual operations. We only need to count when we start NEW operations, not when we continue or end existing ones.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array, each element processed once

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only need a few variables to track the current operation levels

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length == target.length โ‰ค 105
  • 1 โ‰ค nums[i], target[i] โ‰ค 108
  • Both arrays have the same length
  • All array elements are positive integers
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
28.0K Views
Medium-High Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen