Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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] = Truemeans sumiis 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 ?
-
Initialization:
table[0] = Truebecause we can always make sum 0 -
For each number: If we can make sum
j, then we can also make sumj + num - Optimization: Skip duplicate numbers to avoid redundant calculations
-
Result: Check if
table[target_sum]isTrue
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.
