Skip to content

Commit

Permalink
Create Combinations.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
rifafatima authored Oct 2, 2022
1 parent 12698ec commit 26801e2
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Combinations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> ans;
vector<int> nums;
vector<int> comb;

for(int i=1;i<=n;i++)
{
nums.push_back(i);
}

combine(k,0,ans,nums,comb);

return ans;
}

void combine(int k, int idx,vector<vector<int>> &ans, vector<int> &nums, vector<int> &comb)
{
if(comb.size()==k)
{
ans.push_back(comb);
return;
}

for(int i=idx;i<nums.size();i++)
{
comb.push_back(nums[i]);
combine(k,i+1,ans,nums,comb);
comb.pop_back();
}

return;
}
};

0 comments on commit 26801e2

Please sign in to comment.