Program to find minimum numbers of function calls to make target array using Python

Suppose we have a function that can perform two operations on an array:

def modify(arr, op, index):
    if op == 0:
        arr[index] += 1
    if op == 1:
        for i in range(len(arr)):
            arr[i] *= 2

We need to find the minimum number of function calls required to transform a zero array into a given target array nums.

Problem Understanding

Given nums = [1,5,3], we start with [0,0,0] and need to reach the target. The strategy is:

  • Operation 0: Increment a specific element by 1
  • Operation 1: Double all elements in the array

Algorithm Approach

The key insight is to work backwards from each target number. For each number, we count:

  • How many increment operations (op=0) are needed
  • How many doubling operations (op=1) are needed

Since doubling affects all elements, we only need the maximum number of doublings required by any element.

Step-by-Step Solution

def solve(nums):
    increments = 0  # Total increment operations needed
    max_doublings = 0  # Maximum doubling operations needed
    
    for num in nums:
        doublings = 0
        
        # Work backwards from target number
        while num > 0:
            if num % 2 == 0:
                # If even, it came from doubling
                num //= 2
                doublings += 1
            else:
                # If odd, it came from increment then doubling
                num -= 1
                increments += 1
        
        # Track maximum doublings needed
        max_doublings = max(max_doublings, doublings)
    
    return increments + max_doublings

# Test with example
nums = [1, 5, 3]
result = solve(nums)
print(f"Minimum function calls needed: {result}")
Minimum function calls needed: 7

How It Works

For nums = [1,5,3]:

  • Number 1: 1 ? 0 (1 increment, 0 doublings)
  • Number 5: 5 ? 4 ? 2 ? 1 ? 0 (2 increments, 2 doublings)
  • Number 3: 3 ? 2 ? 1 ? 0 (2 increments, 2 doublings)

Total increments: 1 + 2 + 2 = 5
Maximum doublings: max(0, 2, 2) = 2
Total operations: 5 + 2 = 7

Trace Example

def solve_with_trace(nums):
    increments = 0
    max_doublings = 0
    
    for i, num in enumerate(nums):
        doublings = 0
        original_num = num
        
        while num > 0:
            if num % 2 == 0:
                num //= 2
                doublings += 1
            else:
                num -= 1
                increments += 1
        
        max_doublings = max(max_doublings, doublings)
        print(f"Number {original_num}: {increments} total increments, {doublings} doublings")
    
    print(f"Total: {increments} increments + {max_doublings} doublings = {increments + max_doublings}")
    return increments + max_doublings

nums = [1, 5, 3]
solve_with_trace(nums)
Number 1: 1 total increments, 0 doublings
Number 5: 3 total increments, 2 doublings
Number 3: 5 total increments, 2 doublings
Total: 5 increments + 2 doublings = 7

Conclusion

The algorithm works by decomposing each target number into increment and doubling operations. Since doublings affect all elements simultaneously, we only count the maximum doublings needed across all numbers, making this an efficient O(n log m) solution where n is array length and m is the maximum value.

---
Updated on: 2026-03-25T21:03:28+05:30

358 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements