From cc5b92593fd38dbf96fd62094aace28cf2f4b11c Mon Sep 17 00:00:00 2001 From: Amit-yadav099 Date: Wed, 16 Oct 2024 13:19:42 +0530 Subject: [PATCH] Added solution for --- Cpp/216_CombinationSum_III.cpp/problem.md | 31 +++++++++++++++ Cpp/216_CombinationSum_III.cpp/solution.cpp | 42 +++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 Cpp/216_CombinationSum_III.cpp/problem.md create mode 100644 Cpp/216_CombinationSum_III.cpp/solution.cpp diff --git a/Cpp/216_CombinationSum_III.cpp/problem.md b/Cpp/216_CombinationSum_III.cpp/problem.md new file mode 100644 index 0000000..b5d3ae5 --- /dev/null +++ b/Cpp/216_CombinationSum_III.cpp/problem.md @@ -0,0 +1,31 @@ + combination Sum-III + +Link of the problem :-https://leetcode.com/problems/combination-sum-iii/description/ + +Find all valid combinations of k numbers that sum up to n such that the following conditions are true: + +Only numbers 1 through 9 are used. +Each number is used at most once. +Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order. + + + +Intutuion: we have to select 'K' numbers from the given vector such that their sum is euqal to given number 'n'. +we need to count such possible pair of numbers and return it. +so we need to check for every possible pair individually ,to do so ,recursion is the first thing that come into the mind. +However,there are also overlapping cases, so do this wouldn't enough to pass all the test cases so we need to improve it by memoization ,i.e, by the use of dynamic programming + + +//conecpt used:- dynamic programming along with the backtracking to cover all the possible cases. + +Example 1: + +Input: k = 3, n = 7 +Output: [[1,2,4]] +Explanation: +1 + 2 + 4 = 7 +There are no other valid combinations. + + + + diff --git a/Cpp/216_CombinationSum_III.cpp/solution.cpp b/Cpp/216_CombinationSum_III.cpp/solution.cpp new file mode 100644 index 0000000..7c935ac --- /dev/null +++ b/Cpp/216_CombinationSum_III.cpp/solution.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +class Solution { +public: + +//we make a 2D vector "ans" to to collect the all the right possible answer given the 1D vector temp +vector>ans; + +//helper function to do the required +void helper(int sum,int start,int k,int n,vector&temp){ + //base case + //when size becomes k , we check whether our sum is equals to n or not + // if it is equal to n, then we got the answer and push the vector temp into the 2D vector ans + // if it is not equals to n, then we return , as this would not be in our answer + if( temp.size()==k){ + if(sum==n){ + ans.push_back(temp);} + + return ; + } + +//here we start the loop form 1 to 9 in order to enquire about the possible cases + for(int i=start;i<=9;i++){ + //option-01 + temp.push_back(i); + helper(sum+i,i+1,k, n,temp); + //here after the work , if for the given indexwe got the answer then wew defintely get saved into 2D vector 'ans' + //then we pop the value for the current index , so that for next iteration we make temp frm the that value + temp.pop_back(); + } +} + + + + vector> combinationSum3(int k, int n) { + vectortemp; + int sum=0; + helper(sum, 1,k, n,temp); + return ans; + } +}; \ No newline at end of file