Minimum Numbers of Function Calls to Make Target Array - Problem
You are given an integer array target. You have an integer array arr of the same length with all values set to 0 initially.
You also have the following modify function:
- You can call
incrementByOne(arr, i): Increment the value ofarr[i]by 1. - You can call
doubleAll(arr): Double the value of each element inarr.
You want to use the modify function to convert arr to target using the minimum number of calls.
Return the minimum number of function calls to make target from arr.
The test cases are generated so that the answer fits in a 32-bit signed integer.
Input & Output
Example 1 — Basic Operations
$
Input:
target = [1,5]
›
Output:
5
💡 Note:
To build [1,5]: Start with [0,0]. Increment arr[0] to get [1,0]. Increment arr[1] to get [1,1]. Double all to get [2,2]. Increment arr[1] to get [2,3]. Double all to get [4,6]. But this is wrong path. Optimal: increment operations = 1+2=3 (from binary 1=1₂, 5=101₂), double operations = max(0,2)=2. Total = 5.
Example 2 — Single Element
$
Input:
target = [2]
›
Output:
2
💡 Note:
To build [2]: 2 in binary is 10₂. Need 1 increment operation (one 1-bit) and 1 double operation (bit at position 1). Total = 1 + 1 = 2.
Example 3 — All Zeros
$
Input:
target = [0,0]
›
Output:
0
💡 Note:
Array is already [0,0], no operations needed.
Constraints
- 1 ≤ target.length ≤ 105
- 0 ≤ target[i] ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code