Skip to content

Commit

Permalink
Merge pull request #76 from ashish01012001/testbranch
Browse files Browse the repository at this point in the history
Added a new question combination sum iii
  • Loading branch information
Google-DSC-TMSL authored Oct 12, 2023
2 parents c2afc88 + 73bbe22 commit a82d5ff
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
| Longest Valid Parentheses | [LeetCode](https://leetcode.com/problems/longest-valid-parentheses/) | [YouTube](https://www.youtube.com/watch?v=VdQuwtEd10M) | [Java](./codes/java/LongestValidParentheses.java) |
| Rotate Image | [LeetCode](https://leetcode.com/problems/rotate-image/) | [YouTube](https://www.youtube.com/watch?v=Y72QeX0Efxw) | [Java](./codes/java/RotateImage.java) |
| Substring with concatenation of all words | [LeetCode](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | [YouTube](https://www.youtube.com/watch?v=_MGzsJ0F8lE) | [Java](./codes/java/SubstringWithConcatenationOfAllWords.java) |
|Largest rectangular sub-matrix having sum divisible by k| [GFG](https://www.geeksforgeeks.org/largest-rectangular-sub-matrix-sum-divisible-k/) | [YouTube](https://www.youtube.com/watch?v=_MGzsJ0F8lE) | [Python](./codes/python/largest_rectangular_sub_matrix_sum_divisbl_by_k) |
|Largest rectangular sub-matrix having sum divisible by k| [GFG](https://www.geeksforgeeks.org/largest-rectangular-sub-matrix-sum-divisible-k/) | [YouTube](https://www.youtube.com/watch?v=_MGzsJ0F8lE) | [Python](./codes/python/largest_rectangular_sub_matrix_sum_divisbl_by_k)
|Combination Sum III |[LeetCode](https://leetcode.com/problems/combination-sum-iii/) |[Youtube](https://youtu.be/rP_K3WJnRR4?si=W1RUODPlWwOSvsnV) |[C++](./codes/cpp/combination_sum_iii.cpp)

<!--End here-->

Expand Down
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 a82d5ff

Please sign in to comment.