L9. Combination Sum II | Leetcode | Recursion | Java | C++

preview_player
Показать описание

Find DSA, LLD, OOPs, Core Subjects, 1000+ Premium Questions company wise, Aptitude, SQL, AI doubt support and many other features that will help you to stay focussed inside one platform under one affordable subscription. Have a hassle free one stop solution for up-skilling and preparing.

Рекомендации по теме
Комментарии
Автор

nothing wrong in watching the video twice or thrice to get the concepts clear.... Totally worth!!!!

anandhisubramaniam
Автор

Striver is so smart in selecting a perfect example case to explain a concept and the explanation always turns out to be great. Striver, I am extremely grateful for your content.

bhaveshkumar
Автор

I gave a complete day to combination 1 problem as the code that I came up with while remembering the subsequence sum K problem was a bit different from his code, that is, I was not doing K - arr[ i ] instead just sending K and using sum variable for sum == K and used couple of more base cases, and then making several recursion trees and all that and finally the overall intuition and the internal working is mapped in my mind that I can actually visualize the flow of recursion and touching base cases, it's completely worth it giving full time to each and every problems discussed here as it takes you to whole another level of conceptual understanding

HarshSingh-qqjf
Автор

For anyone who is confused between i and idx, Let me explain
idx is that index which tells what value to keep in the data structure and i is just traversal of array
example : 14:27 When function is f(2, 2, ds(1, 1)) it calls for f(3, 1, ds(1, 1, 1)) (here i==idx) then it returns since value is exceeding the target . after it calls for f(4, 0, ds(1, 1, 2) here i>idx but arr[i]!=arr[i-1] meaning, this is the first time we are encountering the element (unique element) . After this call function does'nt goes to f(4, 0, 1, 1, 2)(i>idx) cause arr[i]==arr[i-1] (duplicate element) so function returns here itself.

pcgaming
Автор

TC: 23:52
code(cpp): 28:05
recursive tree : 18:07

arkasarkar
Автор

How can you teach so swiftly, that a noob can also understand it without any doubt. You're amazing Striver, God bless you.

anshumanpanigrahi
Автор

Beautiful, never watched anyone solving good level recursion problem this much smoothly :)

tannukumari
Автор

Let me complete it for him 22:28 : Toh fourth kya ghanta pick krenge... '😂
Thanks for for the amazing video bhaiya...

StevenSteel
Автор

I have been following Striver for the last one year. I just watched the video till 15:31 and was able to code it on my a ton Striver

stith_pragya
Автор

* This solution is same as "Print all the subsets of the given array, but without duplicates".
* Why means, here the combination and there the subset sounds similar.
* We can say like "Print all the subsets with sum 'target' of the given array without duplicate subsets".

For printing all the unique subset, we would have got each unique subset at each recursion node and print them all.
But here though, it is enough to print the subset with a particular sum.

In this way, the Subset II and Combination Sum II looks pretty much similar :)

euit-VishnuR
Автор

Golden knowledge at 18:30
Many people think that just after solving these 2 problems they can crack interviews. NO. Instead of memorizing problems, memorize patterns of the problems. If you are able to understand patterns, you will be able to solve any problem like this problem in the future. Its all about pattern recognition!

rajputadi
Автор

Done on 2 May 2025 at 13:25
Place : Study Room 2, Hostel 5, IIT Bombay

Was Busy due to endsems, Now as summer vacations are starting, i will have around 3 months. Let's see how fast do I complete this series.

ErenYeager-dper
Автор

you are the greatest teacher in the field of DSA trust me.

mastfooter
Автор

It demands your complete attention, grateful to have a mentor like you striver sir.

ShreySrivastava-mc
Автор

Sir, I never seen such a great level of teaching but I can say ur the one of the best and great tutor.
Thank you sir.

chandrakanthakula
Автор

Note that we choose elements only from the right in order to avoid choosing duplicate candidates.

parthsalat
Автор

alternative c++ code :
⭐temp contains candidates in sorted order

void make_set(vector<vector<int>>&vec, vector<int>&curr, vector<int>&temp, int i, int target){
if(target==0){
vec.push_back(curr);
return;
}
if(i ==

curr.push_back(temp[i]);
make_set(vec, curr, temp, i+1, target-temp[i]);

➡//first we add the element and call the function to add next element

curr.pop_back();

➡//remove that element and try next element

while(i+1 != temp.size() && temp[i]==temp[i+1])i++;

➡//checking if the elements are same or not

make_set(vec, curr, temp, i+1, target);

}

vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> vec;
vector<int> curr;
vector<int> temp = candidates;
sort(temp.begin(), temp.end());
make_set(vec, curr, temp, 0, target);
return vec;
}

rishavjain
Автор

if (arr[i] > target) return; This line effectively stops the function from continuing any further in this iteration of the loop, as all subsequent elements will also be too large due to the sorted nature of the array.

sakthibalang
Автор

Fun fact : this video is all about to differentiate "ind" and "i"

arshdeep
Автор

#include<bits/stdc++.h>
using namespace std;

void findCombination(int ind, int target, vector<int> &arr, vector<vector<int>> &ans, vector<int> &ds) {
if (target == 0) {
ans.push_back(ds);
return;
}

if (ind >= arr.size()) return;

if (target >= arr[ind]) {
ds.push_back(arr[ind]);
findCombination(ind + 1, target - arr[ind], arr, ans, ds);
ds.pop_back();
}

int j = ind + 1;
while (j < arr.size() && arr[j] == arr[ind]) j++;
findCombination(j, target, arr, ans, ds);
}

vector<vector<int>> combinationSum2(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> ans;
vector<int> ds;
findCombination(0, target, candidates, ans, ds);
return ans;
}
My code

Aman-hetn
visit shbcf.ru