-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from ashish01012001/testbranch
Added a new question combination sum iii
- Loading branch information
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |