Program to find maximum sum of multiplied numbers in Python

Given two lists nums and multipliers, we need to find the maximum sum by pairing and multiplying elements from both lists. We can remove any number from either list and multiply them together, repeating until one list is empty.

The key insight is to pair negative multipliers with the smallest numbers and positive multipliers with the largest numbers to maximize the sum.

Algorithm

To solve this problem, we follow these steps:

  • Sort both lists in ascending order

  • Ensure nums is the longer list (swap if needed)

  • For each multiplier:

    • If multiplier ? 0: pair with smallest available number

    • If multiplier > 0: pair with largest available number

Example

Let's see the implementation with a step−by−step example ?

def solve(nums, multipliers):
    nums.sort()
    multipliers.sort()
    res = 0
    
    # Ensure nums is the longer list
    if len(nums) < len(multipliers):
        nums, multipliers = multipliers, nums
    
    n, m = len(nums), len(multipliers)
    
    for i in range(m):
        if multipliers[i] <= 0:
            # Pair negative multipliers with smallest numbers
            res += nums[i] * multipliers[i]
        else:
            # Pair positive multipliers with largest numbers
            res += multipliers[i] * nums[n - (m - i)]
    
    return res

# Test the function
nums = [-4, 4, 3]
multipliers = [-2, 2]
result = solve(nums, multipliers)
print(f"Maximum sum: {result}")

# Show the pairing process
print("\nStep-by-step:")
print(f"Sorted nums: {sorted(nums)}")
print(f"Sorted multipliers: {sorted(multipliers)}")
print("Pairing: -4 × -2 = 8, 4 × 2 = 8")
print("Total: 8 + 8 = 16")
Maximum sum: 16

Step-by-step:
Sorted nums: [-4, 3, 4]
Sorted multipliers: [-2, 2]
Pairing: -4 × -2 = 8, 4 × 2 = 8
Total: 8 + 8 = 16

How It Works

The algorithm works by optimally pairing elements:

  • Negative multipliers: Paired with smallest numbers (most negative) to create positive products

  • Positive multipliers: Paired with largest numbers to maximize positive products

  • Sorting: Ensures we can systematically pick optimal pairs

Another Example

# Test with different inputs
nums = [1, 2, 3, 4, 5]
multipliers = [-3, -1, 2]

result = solve(nums, multipliers)
print(f"Maximum sum: {result}")

# Show the logic
sorted_nums = sorted(nums)
sorted_multipliers = sorted(multipliers)
print(f"\nSorted nums: {sorted_nums}")
print(f"Sorted multipliers: {sorted_multipliers}")
print("Optimal pairing:")
print("1 × -3 = -3 (smallest num with most negative multiplier)")
print("2 × -1 = -2 (next smallest with less negative)")
print("5 × 2 = 10 (largest num with positive multiplier)")
print("Total: -3 + (-2) + 10 = 5")
Maximum sum: 5

Sorted nums: [1, 2, 3, 4, 5]
Sorted multipliers: [-3, -1, 2]
Optimal pairing:
1 × -3 = -3 (smallest num with most negative multiplier)
2 × -1 = -2 (next smallest with less negative)
5 × 2 = 10 (largest num with positive multiplier)
Total: -3 + (-2) + 10 = 5

Conclusion

This greedy algorithm achieves optimal results by sorting both lists and strategically pairing negative multipliers with small numbers and positive multipliers with large numbers. The time complexity is O(n log n) due to sorting.

---
Updated on: 2026-03-26T15:50:35+05:30

424 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements