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 N can be represented as sum of integers chosen from set {A, B} in Python
Sometimes we need to check if a target number can be represented as a sum of two given integers A and B, where we can use each integer any number of times. This is a classic dynamic programming problem that can be solved using memoization.
Problem Understanding
Given a target number and two integers A and B, we need to determine if we can express the target as a linear combination of A and B with non-negative coefficients. For example, if target = 26, A = 5, and B = 7, we can achieve this as 26 = 7 + 7 + 7 + 5 (using B three times and A once).
Algorithm Approach
We use a recursive approach with memoization to explore all possible combinations:
- Start from 0 and try adding A or B at each step
- Use a boolean array to track visited sums (memoization)
- If we exceed the target, backtrack
- If we reach the target exactly, return True
Implementation
def util(x, a, b, is_ok, target):
if x > target:
return
if is_ok[x]:
return
is_ok[x] = True
util(x + a, a, b, is_ok, target)
util(x + b, a, b, is_ok, target)
def solve(target, a, b):
is_ok = [False] * (target + 1)
util(0, a, b, is_ok, target)
return is_ok[target]
# Test the function
target = 26
A = 5
B = 7
result = solve(target, A, B)
print(f"Can {target} be represented as sum of {A} and {B}? {result}")
Can 26 be represented as sum of 5 and 7? True
How It Works
The algorithm works by exploring all possible sums starting from 0. At each step, we can either add A or B to our current sum. The is_ok array serves as memoization to avoid redundant calculations for the same sum value.
Alternative Approach Using Mathematical Insight
For cases where A and B are coprime (their GCD is 1), there's a mathematical theorem that any sufficiently large number can be represented. Here's a more direct approach ?
import math
def can_represent_mathematical(target, a, b):
# Handle edge cases
if target == 0:
return True
if target < 0:
return False
# Try all possible multiples of 'a'
for i in range(target // a + 1):
remaining = target - i * a
if remaining % b == 0:
return True
return False
# Test both approaches
target = 26
A = 5
B = 7
result1 = solve(target, A, B)
result2 = can_represent_mathematical(target, A, B)
print(f"Recursive approach: {result1}")
print(f"Mathematical approach: {result2}")
Recursive approach: True Mathematical approach: True
Comparison of Methods
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Recursive + Memoization | O(target) | O(target) | General understanding |
| Mathematical | O(target/min(A,B)) | O(1) | Efficiency |
Conclusion
Both approaches solve the problem effectively. The recursive method provides better insight into the problem structure, while the mathematical approach is more efficient for large targets. Choose based on your specific requirements and constraints.
