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:

You want to use the modify function to covert 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.

&nbsp;
Example 1:

Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -&gt; [0, 2] -&gt; [0, 4] (2 operations).
Increment by 1 (both elements)  [0, 4] -&gt; [1, 4] -&gt; [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.


Example 2:

Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -&gt; [0, 1] -&gt; [1, 1] (2 operations).
Double all the elements: [1, 1] -&gt; [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.


Example 3:

Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -&gt; [1,0,0] -&gt; [1,0,1] -&gt; [2,0,2] -&gt; [2,1,2] -&gt; [4,2,4] -&gt; [4,2,5](nums).


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	0 &lt;= nums[i] &lt;= 109


# Observe that:
#   +1  places a 1 to the end of the int's binary representation
#       (assuming a 0 there previously)
#   x2  is a bitshift left
# So you basically just need to count all the ones in the binary representations
# and find how many shifts are required (largest bit length minus one).

class Solution:
    def minOperations(self, nums: List[int]) -> int:
        if max(nums) == 0:
            return 0
        
        return sum([x.bit_count() for x in nums]) + max([x.bit_length() for x in nums]) - 1
class Solution {
    public int minOperations(int[] nums) {
        int odd = 0, even = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int n : nums){
            int res = dfs(n, map);
            odd += res >> 5;
            even = Math.max(res & 0b11111, even);
        }

        return odd + even;
    }

    private int dfs(int n, Map<Integer, Integer> map){
        if (n == 0) return 0;
        if (map.containsKey(n)) return map.get(n);

        int res = n % 2 << 5;
        res += dfs(n / 2, map) + (n > 1? 1 : 0);

        map.put(n, res);
        return res;
    }
}
class Solution {
public:
    int minOperations(vector<int>& nums) {
        int ans = 0;
        int t = 33;
        while(t--){
            int flag = false;
            for(int i = 0; i<nums.size(); i++){
                if(nums[i]%2) ans++;
                nums[i]/=2;
                if(nums[i]!=0) flag = true;
            }
			
            if(!flag) break;
            ans++;
        }
        return ans;
    }
};
var minOperations = function(nums) {
    let maxpow = 0, ans = 0, pow, val
    for (let i = 0; i < nums.length; i++) {
        for (val = nums[i], pow = 0; val > 0; ans++)
            if (val % 2) val--
            else pow++, val /= 2
        ans -= pow
        if (pow > maxpow) maxpow = pow
    }
    return ans + maxpow
};
Minimum Numbers of Function Calls to Make Target Array