Minimum Numbers of Function Calls to Make Target Array

You are given an integer array nums. You have an integer array arr of the same length with all values set to 0 initially. You also have the following modify function:

  • Increment any single element of arr by 1.
  • Double all elements of arr.

You want to use the modify function to convert arr to nums using the minimum number of calls.

Return the minimum number of function calls to make nums from arr.

The test cases are generated so that the answer fits in a 32-bit signed integer.

Example 1
Inputnums = [1,5]
Output5
The target can be reached with 1 increment on the second element, 2 double operations, and 2 more increments, for a total of 5 operations.
Example 2
Inputnums = [2,2]
Output3
Incrementing both elements once takes 2 operations, then doubling all elements once reaches [2, 2], for a total of 3 operations.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate