Minimum Numbers of Function Calls to Make Target Array - Problem
Transform Array with Minimum Operations

You're given an integer array nums and need to transform an array of zeros into this target array using the minimum number of operations.

Starting with an array arr of the same length filled with zeros, you can perform two types of operations:

Operation 1: Increment any single element by 1
Operation 2: Double all elements in the array

Your goal is to find the minimum total number of operations needed to transform the zero array into the target array nums.

Example:
Target: [1, 5]
• Start: [0, 0]
• Increment index 0: [1, 0] (1 operation)
• Increment index 1: [1, 1] (1 operation)
• Double all: [2, 2] (1 operation)
• Double all: [4, 4] (1 operation)
• Increment index 1: [4, 5] (1 operation)
• Increment index 0 three times: [1, 5] (3 operations)
Wait, that's not optimal! There's a clever greedy approach using bit manipulation.

Input & Output

example_1.py — Basic case
$ Input: [1, 5]
Output: 5
💡 Note: For target [1,5]: Number 1 has binary 001 (1 bit set), number 5 has binary 101 (2 bits set). Total 1-bits = 3 increment operations. Highest bit position is 2, so we need 2 double operations. Total: 3 + 2 = 5 operations.
example_2.py — All zeros
$ Input: [0, 0, 0]
Output: 0
💡 Note: Target array is already all zeros, so no operations needed.
example_3.py — Powers of 2
$ Input: [1, 2, 4]
Output: 5
💡 Note: 1=001₂ (1 bit), 2=010₂ (1 bit), 4=100₂ (1 bit). Total 1-bits = 3. Max bit position = 2 (from 100₂). Total: 3 + 2 = 5 operations.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 0 ≤ nums[i] ≤ 109
  • The answer is guaranteed to fit in a 32-bit signed integer

Visualization

Tap to expand
Binary Construction VisualizationTarget: [1, 5]1 →0011 bit5 →1012 bitsTotal: 3 incrementsBit PositionsPosition 0: 1,5 both havePosition 1: nonePosition 2: only 5 hasMax position: 2Final AnswerIncrements: 3Doublings: 2Total: 5 operationsConstruction Process:[0,0]Start+1s3 increments×2s2 doublings[1,5]Result🎯 Key Insight: Binary representation directly shows the optimal construction path!• Each 1-bit = one increment operation needed• Highest bit position = number of doubling operations needed• This works because we can arrange operations optimally
Understanding the Visualization
1
Convert to Binary
Each target number reveals its construction pattern in binary
2
Count Set Bits
Every '1' bit represents a required increment operation
3
Find Maximum Height
Highest bit position determines how many global doubling operations we need
4
Calculate Total
Sum of all increment operations plus maximum doubling operations
Key Takeaway
🎯 Key Insight: Binary representation reveals the optimal construction strategy - count all 1-bits for increments, find highest bit position for doublings!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
18.4K Views
Medium Frequency
~15 min Avg. Time
856 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