From 2aa46caf2092b7d7e614ced432ca483f3b1a7adc Mon Sep 17 00:00:00 2001 From: Rohan Gore <39983195+rohangore1999@users.noreply.github.com> Date: Fri, 10 Jun 2022 18:12:05 +0530 Subject: [PATCH] Same I tried by seen your video. --- Recursion/lecture 13/Problem statement | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Recursion/lecture 13/Problem statement b/Recursion/lecture 13/Problem statement index 721d85b..805ac95 100644 --- a/Recursion/lecture 13/Problem statement +++ b/Recursion/lecture 13/Problem statement @@ -3,3 +3,47 @@ Problem Statement You are given an array ‘Arr’ of ‘N’ positive integers. You are also given a positive integer ‘target’. Your task is to find all unique combinations of the array ‘Arr’ whose sum is equal to ‘target’. Each number in ‘Arr’ may only be used once in the combination. Elements in each combination must be in non-decreasing order and you need to print all unique combinations in lexicographical order. + + +class Solution(object): + def combinationSum2(self, candidates, target): + + def helper(i,candidates,target,ans,subset,sums,n): + #base conditions:- + #1) if the sum equals to target then append and return + if sums == target: + ans.append(subset[:]) #appending copy of list, cos at last list will be empty + return + + #2) if i goes out of bound; then return + if i == n: + return + + #3) if sums exceeds the target; then return + if sums > target: + return + + #pick + subset.append(candidates[i]) + sums += candidates[i] + helper(i+1,candidates,target,ans,subset,sums,n) + + #backtracking + subset.pop() + sums -= candidates[i] + + #not pick + #to avoid duplicate elements + while i