Skip to content

Commit 06e13e5

Browse files
authored
Merge pull request #337 from bhagyashree-06/master
added combinational sum
2 parents 202cc4c + 811a33a commit 06e13e5

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
DESCRIPTION :
3+
Given an array of integers arr[] and a target number k, write a program to find all unique combinations in arr[]
4+
such that the sum of all integers in the combination is equal to k.
5+
The same repeated number may be chosen from arr[] unlimited number of times.
6+
7+
NOTE :
8+
1. All numbers will be positive integers.
9+
2. Elements in a combination (a1, a2, …, ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
10+
3. The combinations themselves must be sorted in ascending order.
11+
4. The solution set must not contain duplicate combinations.
12+
13+
EXAMPLE :
14+
15+
Given array 2,3,6,7 and target 7,
16+
A solution set is:
17+
[2, 2, 3]
18+
19+
CONSTRAINTS:
20+
1 <= N <= 30
21+
1 <= A[i] <= 20
22+
1 <= B <= 100
23+
24+
SOLUTION APPROACH :
25+
26+
In every recursion run, you either include the element in the combination or you don’t.
27+
To account for multiple occurrences of an element,
28+
make sure you call the next function without incrementing the current index.
29+
30+
"""
31+
# SOLUTION IN PYTHON3
32+
33+
34+
class Solution:
35+
# @param A : list of integers
36+
# @param B : integer
37+
# @return a list of list of integers
38+
def combinationSum(self, A, B):
39+
# Remove duplicate and sort since each element can be repeated
40+
# in combination either way.
41+
# Removing duplicates here helps us avoid removing them from the final result.
42+
A=sorted(set(A))
43+
# if the target sum is less than 2 or the lis A is empty,
44+
# then retirn an empty list
45+
if B<2 or len(A)==0:
46+
return []
47+
48+
49+
result=[]
50+
comb=[]
51+
index=0
52+
self.find(result, comb, A, B, index)
53+
return result
54+
55+
def find(self, result, comb, A, B, index):
56+
# The recursion will nest until we hit the target. At this point we append and return.
57+
if sum(comb) == B:
58+
result.append(comb[:])
59+
return
60+
# If the sum is bigger, we also terminate the recursion chain.
61+
if sum(comb)>B:
62+
return
63+
64+
for i in range(index, len(A)):
65+
comb.append(A[i])
66+
# Start from same index since an element can be repeated.
67+
self.find(result, comb, A, B, i)
68+
comb.pop()
69+
70+
'''
71+
72+
Sample Input : A = [2,3,5], B = 8
73+
Output:
74+
[
75+
[2,2,2,2],
76+
[2,3,3],
77+
[3,5]
78+
]
79+
80+
EXPLANATION:
81+
All the unique combinations whose sum is 8 is printed.
82+
2+2+2+2 = 8
83+
2+3+3 = 8
84+
3+5 = 8
85+
86+
COMPLEXITY ANALYSIS :
87+
88+
Time Complexity -> O(l^k)
89+
Where l is the length of the array, k is the length of the longest possible combination (namely target / minInArray).
90+
91+
Space Complexity -> O(k)
92+
for storing the path, which could be k long at most.
93+
94+
'''

0 commit comments

Comments
 (0)