Skip to content

Commit

Permalink
Added a new question combination sum iii
Browse files Browse the repository at this point in the history
  • Loading branch information
ashish01012001 authored Oct 11, 2023
1 parent f601e22 commit 73bbe22
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions codes/cpp/combination_sum_iii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
#include<vector>
using namespace std;

void findProperSubSet(int k,int n,vector<vector<int>>&ans,vector<int>&partAns,int l){
if(k==0&&n<0)
return;
if(k==0&&n==0){
ans.push_back(partAns);
return;
}
if(k==0&&n>0)
return;
for(int i=l;i<=9;i++){
partAns.push_back(i);
findProperSubSet(k-1,n-i,ans,partAns,i+1);
partAns.pop_back();
}

}
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>>ans;
vector<int>partAns;
findProperSubSet(k,n,ans,partAns,1);
return ans;
}
int main()
{
int k,n;
cin>>k;
cin>>n;
vector<vector<int>>ans=combinationSum3(k,n);
for(int i=0;i<ans.size();i++){
for(int j=0;j<k;j++){
cout<<ans[i][j];
}
cout<<""<<endl;
}

return 0;
}

0 comments on commit 73bbe22

Please sign in to comment.