-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combination Sum II(Tle).cpp
80 lines (62 loc) · 1.61 KB
/
Combination Sum II(Tle).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//{ Driver Code Starts
//Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution{
public:
void solve(vector<vector<int>>& ans, vector<int> temp,vector<int>& arr, int n , int k ,
int index , int currSum){
if(currSum>k) return ;
if(index>=n || currSum==k){
if(currSum == k ) {
// mapping[currMapIndex++]=temp;
ans.push_back(temp);
} return ;
}
solve(ans,temp,arr,n,k,index+1,currSum);
temp.push_back(arr[index]);
solve(ans,temp,arr,n,k,index+1,currSum+arr[index]);
return ;
}
vector<vector<int>> CombinationSum2(vector<int> arr,int n,int k)
{
//code here
vector<vector<int>>ans;
int currMapIndex=0;
vector<int>temp;
solve(ans,temp,arr,n,k,0,0);
sort(ans.begin(),ans.end());
return ans;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
vector<int> arr(n);
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
Solution ob;
auto obj=ob.CombinationSum2(arr,n,k);
for(int i=0;i<obj.size();i++)
{
for(int j=0;j<obj[i].size();j++)
{
cout<<obj[i][j]<<" ";
}
cout<<"\n";
}
if(obj.size()==0) cout<<"\n";
}
return 0;
}
// } Driver Code Ends