Check if is possible to get given sum from a given set of elements in Python

Given an array of numbers and a target sum, we need to check if it's possible to achieve the target sum by adding elements from the array. Elements can be used multiple times.

For example, with nums = [2, 3, 5] and sum = 28, the answer is True because we can get 28 using: 5 + 5 + 5 + 5 + 3 + 3 + 2 = 28.

Approach

We'll use dynamic programming with a boolean table to track all possible sums that can be formed ?

  • Create a boolean table where table[i] = True means sum i is achievable
  • Initialize table[0] = True (sum 0 is always possible with no elements)
  • For each number in the array, update all possible sums by adding that number
  • Check if the target sum is achievable

Implementation

MAX = 1000
table = [False] * MAX

def util(nums):
    # Reset table and mark 0 as achievable
    for i in range(MAX):
        table[i] = False
    table[0] = True
    
    # Remove duplicates and sort for optimization
    nums = sorted(set(nums))
    
    for num in nums:
        # Skip if this number itself is already achievable
        if table[num]:
            continue
            
        # Update table for all possible sums using this number
        for j in range(MAX - num):
            if table[j]:
                table[j + num] = True

def solve(nums, target_sum):
    util(nums)
    return table[target_sum]

# Test the solution
nums = [2, 3, 5]
target_sum = 28
result = solve(nums, target_sum)
print(f"Can achieve sum {target_sum}: {result}")

# Verify with another example
nums2 = [1, 4, 6]
target_sum2 = 8
result2 = solve(nums2, target_sum2)
print(f"Can achieve sum {target_sum2}: {result2}")
Can achieve sum 28: True
Can achieve sum 8: True

How It Works

The algorithm builds up all possible sums incrementally ?

  1. Initialization: table[0] = True because we can always make sum 0
  2. For each number: If we can make sum j, then we can also make sum j + num
  3. Optimization: Skip duplicate numbers to avoid redundant calculations
  4. Result: Check if table[target_sum] is True

Alternative Recursive Approach

Here's a simpler recursive solution with memoization ?

def can_sum(nums, target, memo={}):
    if target in memo:
        return memo[target]
    if target == 0:
        return True
    if target < 0:
        return False
    
    for num in nums:
        if can_sum(nums, target - num, memo):
            memo[target] = True
            return True
    
    memo[target] = False
    return False

# Test the recursive approach
nums = [2, 3, 5]
target = 28
result = can_sum(nums, target)
print(f"Recursive approach - Can achieve sum {target}: {result}")
Recursive approach - Can achieve sum 28: True

Comparison

Approach Time Complexity Space Complexity Best For
Dynamic Programming O(MAX × n) O(MAX) Multiple queries with same array
Recursive + Memoization O(target × n) O(target) Single query, large MAX limit

Conclusion

The dynamic programming approach efficiently determines if a target sum is achievable using array elements with repetition allowed. The recursive solution with memoization provides a more intuitive alternative for smaller target values.

Updated on: 2026-03-25T15:08:58+05:30

510 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements