Ep10- Subset II problem | Find all UNIQUE subsets of an array with duplicate elements | Recursion

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024
  • In the lecture number 7 of the recursion series, we saw how to find all the subsets of an array, but in this lecture we will see, how to find all UNIQUE subsets of an array with duplicate elements. Make sure that you watch lecture no.7 before watching this video.
    Practice - www.codingninj...
    Code available in Cpp, Java, Python - github.com/Lea...
    Pre-requisites:-
    1. Ep7 - Find all the Subsets of an array
    • Ep7 - Find all the pos...
    Main channel link - bit.ly/3GBAV7f
    DSA Placement series - • DSA Placement series
    Recursion playlist - • Recursion
    🔴Watch the complete Linked List Series • Linked List Series
    To get all updates follow on Instagram
    / frazmohammad
    👨‍💻Utilize free courses, DSA sheet, and articles on Lead Coding website at
    Join our telegram channel for all the important announcements
    t.me/leadcoding
    ✨ Hashtags ✨
    #DSA #Recursion #Placement #Fraz

ความคิดเห็น • 149

  • @arghya_0802
    @arghya_0802 2 ปีที่แล้ว +37

    Summary of the lecture:
    1. In Subsets-I, we have all the elements as unique. That's why our Pick and Don't Pick conditions were working fine.
    2. In Subsets-II, we will have duplicate elements. But we want our final ans to contain only unique subsets.
    3. We can achieve it by counting only one occurrence of an element and skipping all its rest occurrences in arr[]
    4. We need to first sort the given vector arr[]
    5. For don't pick condition, we will first check whether arr[i]==arr[i+1] or not. If so, we will ignore the rest occurrences of that particular element.
    5. Time and Space Complexity will remain same[in worst case we will have unique elements only]

    • @LearnYardYT
      @LearnYardYT  2 ปีที่แล้ว +5

      Good job🚀

    • @arghya_0802
      @arghya_0802 2 ปีที่แล้ว +1

      @@LearnYardYT Thank you sir

    • @arghya_0802
      @arghya_0802 2 ปีที่แล้ว +1

      @@avaneshwari666 Sets will definitely work ma'am but the main issue is that it will consume a lot of time everytime we use find() function , so a better approach is to just skip all the duplicate elements. In this way both our time and space complexity is increased

    • @aveermukherjee6419
      @aveermukherjee6419 2 ปีที่แล้ว +3

      @@avaneshwari666 Mam set can also work and the time complexity will remain O(2^n) using the pick and non pick algo for the subset with some minor changes. This is the code below with some minor changes :-
      #include
      void subsets(int i,int n,vector &arr,vector &subset,vector &ans_set)
      {
      //Base case
      if(i==n)
      {
      ans_set.push_back(subset);
      }
      else
      {
      subset.push_back(arr[i]);
      subsets(i+1,n,arr,subset,ans_set);
      subset.pop_back();
      subsets(i+1,n,arr,subset,ans_set);
      }
      }
      vector uniqueSubsets(int n, vector &arr)
      {
      // Write your code here.
      sort(arr.begin(),arr.end());
      vector subset;
      vector ans_set;
      subsets(0,n,arr,subset,ans_set);
      set ans_dup;
      for(auto i:ans_set)
      {
      ans_dup.insert(i);
      }
      ans_set.clear();
      for(auto i:ans_dup)
      {
      ans_set.push_back(i);
      }
      return ans_set;
      }

    • @aveermukherjee6419
      @aveermukherjee6419 2 ปีที่แล้ว +2

      @@avaneshwari666 Welcome mam

  • @mukeshkir1991
    @mukeshkir1991 2 ปีที่แล้ว +11

    When someone says how to master recursion then i send them the link of your channel. The best playlist of recursion on youtube.........thank you fraz bhaiya for providing these quality lectures.......lots of thank you ..My IDEAL..🙏🙏🙏🙏

    • @pryansh_
      @pryansh_ ปีที่แล้ว +1

      and then you will have people like me who will surpass your leetcode profile using links and will flaunt it to you only in particular.. doesn't it itches your balls..

  • @avinashb5055
    @avinashb5055 29 วันที่ผ่านมา

    Hey, Thanks for this playlist. I always had issues in understanding recursion problems. The main thing I liked about your playlist is that you have added prerequisites for all the videos. If I'm not able to understand something I know what I'm lacking currently and can refer the videos in the prerequisites. This made even the concepts I never understood over months of leetcoding now. Thanks a lot !

  • @BurhanAijaz
    @BurhanAijaz 2 ปีที่แล้ว +21

    Day 10 🔥.
    only 30% of the people are able to be consistent for 10 days or more and if u are one of them , give urself a pat on the back , and good luck for the rest of the course , hopefully we will be together for this entire journey 💯

  • @InfiniteCuriosity612
    @InfiniteCuriosity612 2 ปีที่แล้ว +2

    I have always feared recursion but your way of teaching has made it so easy. Thank you so much for this extremely well explained video.

  • @sword013
    @sword013 หลายเดือนก่อน

    I did not understand this from other's solutions. But this approach made me understand, its much easier and intuitive

  • @ranitbandyopadhyay
    @ranitbandyopadhyay 2 ปีที่แล้ว +1

    day by day, lectures are getting more interesting. Good video. Waiting for tomorrow.

  • @priyanshusrivastava2145
    @priyanshusrivastava2145 ปีที่แล้ว +1

    just learned a new way to approach this problem.. best one till now

  • @Ji-yoon
    @Ji-yoon 2 ปีที่แล้ว +1

    Bhaiya thank you so much........I can say this is the best playlist of recursion so far !!!!!

  • @de_ansh
    @de_ansh 2 ปีที่แล้ว

    In the beginning, I was not able to understand, I have also commented on the 7th video that it's difficult to understand, but today even before the video started I was able to code a recursive on my own, That's because of you bhiaya.
    Thank you so much bhaiya.

  • @arghya_0802
    @arghya_0802 2 ปีที่แล้ว +5

    14:54
    Worst Case Time Complexity: O(2^N)
    Space Complexity:
    O(N)
    This is mainly because in the Worst Case we will have all unique elements and the maximum number of nodes will be 2^N , calculated by sum of GP series( explained in lecture 7)

    • @srajansohani464
      @srajansohani464 2 ปีที่แล้ว +2

      we can use a map to store the vector we have created till now by picking or not picking and can ignore repeated ones as they will be same so map will consider them one

    • @arghya_0802
      @arghya_0802 2 ปีที่แล้ว +1

      @@srajansohani464 Yes we definitely can but that will increase our Time Complexity and might give us TLE. So we prefer to use it this way using While Loop

    • @adanulabidin
      @adanulabidin 4 หลายเดือนก่อน

      ​@@srajansohani464 I had the same intuition. In fact , I have even implemented it this way and commented it here somewhere. Hope you find it 😂

  • @it_08amanagarwal35
    @it_08amanagarwal35 2 ปีที่แล้ว +2

    If we are using a set in these type of questions then the time complexity will not reduce so these type of optimizations helps us to understand code better thanks for selecting best questions in sequence every day❤❤❤❤❤❤❤❤❤

    • @aveermukherjee6419
      @aveermukherjee6419 2 ปีที่แล้ว +1

      Set wont work here. It will give error. I will explain with an example here.
      For eg take an array=[5,5,3,5].
      If you run the program using set it will produce an output =
      3
      3 5
      5
      5 3
      5 3 5
      5 5
      5 5 3
      5 5 3 5
      5 5 5
      If you run the program using vector it will produce the output =
      5 5 3 5
      5 5 3
      5 5 5
      5 5
      5 3 5
      5 3
      5 5
      5
      5 3 5
      5 3
      5 5
      5
      3 5
      3
      5
      Now if you observe them carefully set considers those two subsets equal or duplicate if they have same element in the same index or position.
      But according to the question they are taking those subsets as equal who have same elements irrespective of their position or indexes.
      i.e according to the question [3,5] and [5,3] are also equal. But set doesnt remove them.
      Therefore the original output for array=[5,5,3,5] is =
      3
      3 5
      3 5 5
      3 5 5 5
      5
      5 5
      5 5 5

  • @VijenderSingh-wr6fm
    @VijenderSingh-wr6fm 4 หลายเดือนก่อน

    watched so many videos on this , but you explained really well brother without making it complex

  • @mohitroshan3856
    @mohitroshan3856 2 ปีที่แล้ว +6

    video is of 16 min but time needed to get it 1-1.5 hours 😅
    Revise previous notes + watch video + Notes + Question practice on code Studio.
    Best part is that we are consistent !!!🤩

    • @LearnYardYT
      @LearnYardYT  2 ปีที่แล้ว +6

      Consistency is the main key here. Many people start, but they leave it when the concepts start getting harder and harder. So stay consistent. This one hour which you have spent now, will play an important role in your journey of mastering recursion.

    • @mohitroshan3856
      @mohitroshan3856 2 ปีที่แล้ว

      @@LearnYardYT haa bhaiya bahot maza aa rha hai😁😁
      Actually this is my first series of any dsa concept on TH-cam 🙏🙏
      Itna maza aa rha ki aapka linked list wala series bhi kar rhe

  • @pratika-prakhar
    @pratika-prakhar 3 หลายเดือนก่อน

    This is very helpful lecture, I watched n number of videos but this make me understand where we are making the duplicate subsets

  • @vinaykamble162
    @vinaykamble162 2 ปีที่แล้ว +2

    Complete Day 10 ❤️
    Similarly working on your oops concepts. Bhaiyya you provided oops sheets enough for placement?... enjoying all you videos with regular consistent ❤️

  • @yashbahuguna8135
    @yashbahuguna8135 2 ปีที่แล้ว +1

    #lecture 10 completed 😊consistency

  • @pryansh_
    @pryansh_ ปีที่แล้ว +1

    thanks, you really helped !!

  • @shagunchoudhary3029
    @shagunchoudhary3029 2 ปีที่แล้ว

    Aaj 6 se phle hi....you are making us more consistent!😎💯 Awesome content smjh aa rha hai ache se, ThankYou Fraz Bhaiya🙋‍♀️🤟thankyou so much.
    WAITING FOR NEXT VIDEO

  • @anuraggulati2167
    @anuraggulati2167 2 ปีที่แล้ว +1

    Lecture 10 Done 💝💝👍👍👌👌👌🙌 waiting for the next Lecture 🤞🤞

  • @rhosen
    @rhosen ปีที่แล้ว

    At last, I've found what I was looking for. Thank you so much.

  • @ratnasanjay
    @ratnasanjay 2 ปีที่แล้ว

    Thankyou bhaiya for helping us learn this for free

  • @anirudrabrahma_0971
    @anirudrabrahma_0971 2 ปีที่แล้ว +5

    Previously:
    DSA...DSA...DSA I don't like it, I avoid.
    Now(after watching your lecture):
    But DSA likes me. I can't avoid 😂

    • @gauravshukla5203
      @gauravshukla5203 ปีที่แล้ว

      can you please explain me why are we skipping duplicate values only in the case of ignoring the element

    • @anirudrabrahma_0971
      @anirudrabrahma_0971 ปีที่แล้ว

      @@gauravshukla5203 we have to find the unique combinations....so think how duplicates are formed..it's by having duplicate elements...so if we ignore duplicate elements..our job is done

  • @productivewind
    @productivewind 2 ปีที่แล้ว +1

    Yes, I submited this question before 1 day. But thank you for this video.

  • @hashshamkhan2105
    @hashshamkhan2105 ปีที่แล้ว

    thanks sir I was really messed with this question you helped me!

  • @karthikm4594
    @karthikm4594 2 ปีที่แล้ว

    Thank you very much Fraz for your efforts to provide quality content. I became fan of your explanation. Keep up good work.

  • @HustlerCoder
    @HustlerCoder ปีที่แล้ว

    Good Explanation

  • @sushant2287
    @sushant2287 2 ปีที่แล้ว +1

    Can you please explain this problem in Java code
    Because logic mentioned in the Java code is completely different
    It would be difficult for you to make seperate video so can you please solve this in Java in doubt solving session
    This would be helpful 🙏🙏

  • @srajansohani464
    @srajansohani464 2 ปีที่แล้ว

    used map to store every subset and not repeated it by checking previous occurrence but your approach is good

  • @aveermukherjee6419
    @aveermukherjee6419 2 ปีที่แล้ว +1

    Sir I appreciate the hardwork you are putting for us and providing such beautiful explanations. But isnt 3 months time very less for this course. If we do 1 question one day then 90 questions will be done in 3 months. Are 90 questions enough for cracking placements?

    • @LearnYardYT
      @LearnYardYT  2 ปีที่แล้ว +2

      One thing we will have to understand, any course would not make you solve 500 questions.
      You have to to solve it, learn from here apply in contests.
      We are also organising contests soo n

  • @therock8815
    @therock8815 2 ปีที่แล้ว

    motivation is very necessary so if possible then every sunday make a video to how to we study this course and some other knowledge

  • @apurbakumarmajumder9478
    @apurbakumarmajumder9478 2 ปีที่แล้ว

    bhaiya your videos are awesome
    bhaiya is recursion series is over or we can expect more videos in this series.

  • @sagarpokhriyal6926
    @sagarpokhriyal6926 2 ปีที่แล้ว

    Level increasing day by day 🌠
    Lecture 10 Completed ☑
    #dsabyfaraz💯
    #Recursion 🌠

  • @hrithikrudra4292
    @hrithikrudra4292 2 ปีที่แล้ว

    Consistently at its best,bhaiya

  • @iamyuvraj128
    @iamyuvraj128 ปีที่แล้ว

    What a explanation. Hat's off to you.

  • @vaishnavirathore5579
    @vaishnavirathore5579 ปีที่แล้ว

    sir your playlist is amazing but please make java related vedios also

  • @nishigupta2501
    @nishigupta2501 2 ปีที่แล้ว

    Very well explained

  • @faizansiddiqui753
    @faizansiddiqui753 2 หลายเดือนก่อน

    Best explanation

  • @satyammishra6356
    @satyammishra6356 2 ปีที่แล้ว

    Great session maza aaya😌

  • @it_08amanagarwal35
    @it_08amanagarwal35 2 ปีที่แล้ว

    In the best case it will helps us to ignore all the redundant work which we are doing

  • @Itsme1n1ly
    @Itsme1n1ly ปีที่แล้ว

    Nice explanation bro 🎉

  • @sidhantkaushal3402
    @sidhantkaushal3402 2 ปีที่แล้ว

    we sorted the array to get all similar elements aligned together and then we just need to add a condition in the recursive function which checks it and skips that occurrence. lec 10 done.

  • @vipulrohilla1383
    @vipulrohilla1383 ปีที่แล้ว

    Thanks Buddy for this wonderful explanation

  • @the_haryannvi_coder
    @the_haryannvi_coder ปีที่แล้ว

    11:55 Fraz, if we sort the given array then we have changed the order of elements in the array. In subsets the main thing to preserve is the order. So, I think we can't sort the array.

  • @sadikahmed3045
    @sadikahmed3045 2 ปีที่แล้ว

    Congratulations 🎉🎉👏👏 for upcoming 10k

  • @RajputAnkit11
    @RajputAnkit11 2 ปีที่แล้ว

    Day 10 done ✅ tomorrow i will revise all the videos and code from day 1 to day 10 ❤️

  • @bhuneshmeravi8411
    @bhuneshmeravi8411 2 ปีที่แล้ว

    Worst case time complexity 2^n
    space complexity n
    order of consistency is constant😊😊

  • @HarshRaj-kp7gk
    @HarshRaj-kp7gk 2 ปีที่แล้ว +1

    #bhaiya
    Bhaiya beginners course kab se start hogi ??

  • @vandanakumari-bm9de
    @vandanakumari-bm9de 10 วันที่ผ่านมา

    Will the additional while loop of skipping elements, not add to the time complexity?

  • @surabhichoubey2987
    @surabhichoubey2987 2 ปีที่แล้ว

    Thank you bhiya well explained

  • @Sandeep-zd6dq
    @Sandeep-zd6dq 2 ปีที่แล้ว +1

    Bhaiya in this question the array need to be sorted na ? 🤔 isn’t it possible to do it in O(n) if array is not sorted ? Maybe it’s possible to do it using hash set

    • @aveermukherjee6419
      @aveermukherjee6419 2 ปีที่แล้ว +1

      hash set wont work here .It will give error. I will explain with an example here.
      For eg take an array=[5,5,3,5].

      If you run the program using hash set it will produce an output =
      3
      3 5
      5
      5 3
      5 3 5
      5 5
      5 5 3
      5 5 3 5
      5 5 5
      If you run the program using vector it will produce the output =
      5 5 3 5
      5 5 3
      5 5 5
      5 5
      5 3 5
      5 3
      5 5
      5
      5 3 5
      5 3
      5 5
      5
      3 5
      3
      5
      Now if you observe them carefully set considers those two subsets equal or duplicate if they have same element in the same index or position.
      But according to the question they are taking those subsets as equal who have same elements irrespective of their position or indexes.
      i.e according to the question [3,5] and [5,3] are also equal. But set doesnt remove them.
      Therefore the original output for array=[5,5,3,5] is =
      3
      3 5
      3 5 5
      3 5 5 5
      5
      5 5
      5 5 5

    • @sandypatel_
      @sandypatel_ ปีที่แล้ว

      2^n to vese bhi lagna hi he recursion me to baki chize nahi sochna or set unique value store karta he to for ex -->3 9 2 3 7 --> isme hame subset k form me (2 3 3 7 9) to chahiye hi set k use se dusra vala 3 lost ho jayega or qn me bola he unique chahiye (2 3 3 7 9) or ye unique he ------>hame duplicate ko count me nahi lena he qn ye bola he-

  • @vishalgowrav
    @vishalgowrav 2 ปีที่แล้ว

    Episode 10 Done!!🔥
    Amazing Explaination ❤️
    Subset II:-
    (When all the elements inside array/string are not unique)
    When we are arent picking the element that to be present inside the subset then make sure that you dont pick the other occurrence of the element we aren't picked,so we need increment i to skip the repeating elements.And to this we use while loop.This is the only difference between Subset I and II

  • @mdsufyankhan1029
    @mdsufyankhan1029 2 ปีที่แล้ว

    Day 10 done ✅✅
    Well going ❤️

  • @amoghasoda
    @amoghasoda ปีที่แล้ว

    This helped a lot! Thanks!

  • @Abhishek-fo3fc
    @Abhishek-fo3fc 2 ปีที่แล้ว +1

    Done understood ❤️✅

  • @dineshgangirredi1072
    @dineshgangirredi1072 ปีที่แล้ว

    Bro, I had a small doubt. Why we need to increment i in the skip part only, why won't before the include part ?

  • @emtiazahmed5333
    @emtiazahmed5333 2 ปีที่แล้ว

    #ep_10 done√√
    Love u bhaiya❤️❤️❤️

  • @nilaanil1061
    @nilaanil1061 2 ปีที่แล้ว

    Thankyou so much!

  • @chiragkarnwal6740
    @chiragkarnwal6740 2 ปีที่แล้ว +1

    Thanks sir 😀😀❤️

  • @isikamaiti9116
    @isikamaiti9116 2 ปีที่แล้ว

    Ep 10 done ✅.Exam khatam ho geya aj ,ab revision Dena parega achhe se.

  • @rajankhunt7002
    @rajankhunt7002 2 ปีที่แล้ว

    Good Video

  • @innoirvinge8431
    @innoirvinge8431 ปีที่แล้ว

    made me subscribe at 5:38

  • @dashundev1586
    @dashundev1586 2 ปีที่แล้ว

    We can also remove duplicates(set) and create subset
    Need to check time complexity for it

  • @gauravshukla5203
    @gauravshukla5203 ปีที่แล้ว

    can you please explain me why are we skipping duplicate values only in the case of ignoring the element?

  • @BurhanAijaz
    @BurhanAijaz 2 ปีที่แล้ว

    what if we convert the given array to set and then back to array and then continue with the same approach as in ep 7, wouldn't that work as well?

  • @shalimar_mehra
    @shalimar_mehra 2 ปีที่แล้ว

    Present ! ✔🙌

  • @akhileshwarpandit5632
    @akhileshwarpandit5632 2 ปีที่แล้ว

    Completed 👍

  • @avinashgahlowt4665
    @avinashgahlowt4665 2 ปีที่แล้ว

    Sir what if we can create a discord server for all the information and discussion/doubts

  • @akshitjain2906
    @akshitjain2906 ปีที่แล้ว

    legend!

  • @mohamedosamag8771
    @mohamedosamag8771 ปีที่แล้ว

    can u pls explain the same code in java? i saw ur repository, there is no code in java for this problem

  • @PoojaYadav-ji4cq
    @PoojaYadav-ji4cq ปีที่แล้ว

    Thankyou🌺

  • @koushikd6935
    @koushikd6935 2 ปีที่แล้ว

    Great

  • @sameerakhatoon9508
    @sameerakhatoon9508 ปีที่แล้ว

    that's dope, ty sm!!

    • @sameerakhatoon9508
      @sameerakhatoon9508 ปีที่แล้ว

      find my recursive code if string w/ duplicate char but consecutively is given
      public class Recursion{
      public static void main(String[] args) {
      System.out.println(subSetsWithDupInString("122", ""));
      }
      static ArrayList subSetsWithDupInString(String str, String ans){
      if(str.isEmpty()){
      ArrayList ls = new ArrayList();
      ls.add(ans);
      return ls;
      }
      char ch = str.charAt(0);
      ArrayList left = new ArrayList();
      ArrayList right = new ArrayList();

      int i = 1;
      while(i

  • @AdityaJain-ed9my
    @AdityaJain-ed9my 5 วันที่ผ่านมา

    thanku

  • @devendrakumargupta9021
    @devendrakumargupta9021 2 ปีที่แล้ว

    Worst case is when all elements are unique. And the best case is when all elements are same. Then we'll skip all the elements and hence time complexity will be no. Of elements permutated.

  • @hiiiiiiii927
    @hiiiiiiii927 2 ปีที่แล้ว

    Day 10 done,
    If elements are unique we need to skip the elements by doing while(i+1 < arr.length && arr[i] == arr[i+1]) and then we need to start the recursion from i+1

  • @rishav144
    @rishav144 2 ปีที่แล้ว

    thanks a lot

  • @saipradeepkatakam9734
    @saipradeepkatakam9734 2 ปีที่แล้ว

    Day 10 completed waiting for tomorrow.

  • @ShivaniSinghYadav-sm3ee
    @ShivaniSinghYadav-sm3ee 2 ปีที่แล้ว

    Present ☀️🌻

  • @ronitroy1391
    @ronitroy1391 2 ปีที่แล้ว

    Why not just use a set to discard dupli at beginning? Can't we do this bhaiya?

  • @neetisaharan1340
    @neetisaharan1340 2 ปีที่แล้ว

    Complete Day 10 ❤️

  • @anisakhanum405
    @anisakhanum405 2 ปีที่แล้ว

    Can't we just use set like we did for unique permutations??

  • @AtomicAkshay
    @AtomicAkshay 2 ปีที่แล้ว

    Notes:
    The question is very much similar to subset 1 problem, the trick is to figure out the logic that a number should not be included in the subset ever once it is skippped.
    Time Complexity: O(2^n) when all elements are unique
    Space Complexity: O(n) For recursion stack, not including space taken up by result array

  • @divyanshsagar
    @divyanshsagar 2 ปีที่แล้ว

    Watched episode 10!

  • @Lucifer-xt7un
    @Lucifer-xt7un 2 ปีที่แล้ว

    Please try to make 2 videos a day bro

  • @amitranjeetjha1240
    @amitranjeetjha1240 2 ปีที่แล้ว

    Day 10 how's the Josh...high sir🔥🔥

  • @maazulhaque7293
    @maazulhaque7293 2 ปีที่แล้ว

    Episode-10 Done

  • @gouravmehra007
    @gouravmehra007 2 ปีที่แล้ว

    Time Complexity will be O(2^n) and O(n)

  • @25-cse-csmohitkumarmandal59
    @25-cse-csmohitkumarmandal59 2 ปีที่แล้ว

    Bhaiya please increase voice quality..very low sound is there

  • @aakashbhargava1
    @aakashbhargava1 2 ปีที่แล้ว

    thank you

  • @aveermukherjee6419
    @aveermukherjee6419 2 ปีที่แล้ว

    We can also solve this question using bitmasking. This is the code :-
    #include
    vector uniqueSubsets(int n, vector &arr)
    {
    // Write your code here.
    sort(arr.begin(),arr.end());
    set set_ans;
    long long bit_length=(1

  • @bhagyavanvansh878
    @bhagyavanvansh878 2 ปีที่แล้ว

    How to sort 2D ArrayList??

  • @amanbhojak8281
    @amanbhojak8281 2 ปีที่แล้ว

    DAY 10 🔥🔥

  • @cse_29_abdulshariqkalim15
    @cse_29_abdulshariqkalim15 2 ปีที่แล้ว

    summary : earlier we were considering one element and skipping one element but in order to find unique subsets we need skip not only ith element but also all i+1 elements if arr[i]==arr[i+1].

  • @abhishekpandey8988
    @abhishekpandey8988 2 ปีที่แล้ว

    Bhaiya aap sbhi topic cover kroge na DSA ke??

    • @mohitroshan3856
      @mohitroshan3856 2 ปีที่แล้ว

      Bhaiya will cover recursion,backtracking,dp,graph,tree in this series and parallel se ek aur series aane wala hai beginner course usme baki topic cover honge.

  • @adanulabidin
    @adanulabidin 4 หลายเดือนก่อน

    Very informative and well explained video @LeadCodingbyFRAZ. I came here to checkout the most optimised solution for this problem although I have already solved it using 2 methods. Sharing them here just in case anyone is curious :)
    Solution 1 (Using map to store the subsets as it only stores unique values [NOTE : only map works for this and not unordered_map if someone knows why then please do let me know]):
    #include
    map mp;
    void helper(int index,int n,const vector& a,vector& curr){
    if(index > n){
    mp[curr]++;
    return;
    }
    //include
    curr.push_back(a[index]);
    helper(index + 1,n,a,curr);
    curr.pop_back();
    //exclude
    helper(index + 1,n,a,curr);
    }
    vector uniqueSubsets(int n, vector &arr)
    {
    vector ans;
    vector curr;
    sort(arr.begin(),arr.end());
    helper(0,n - 1,arr,curr);
    for(auto& it: mp){
    vector x = it.first;
    ans.push_back(x);
    }
    mp.clear();
    return ans;
    }
    Solution 2 (Finding out all the possible subsets and then removing the duplicates using unique( ) ) :
    #include
    void helper(int index,int n,const vector& a,vector& curr,vector& ans){
    if(index > n){
    ans.push_back(curr);
    return;
    }
    //include
    curr.push_back(a[index]);
    helper(index + 1,n,a,curr,ans);
    curr.pop_back();
    //exclude
    helper(index + 1,n,a,curr,ans);
    }
    vector uniqueSubsets(int n, vector &arr)
    {
    vector ans;
    vector curr;
    vector a = arr;
    sort(a.begin(),a.end());
    helper(0,n - 1,a,curr,ans);
    sort(ans.begin(),ans.end());
    auto it = unique(ans.begin(),ans.end());
    ans.erase(it,ans.end());
    return ans;
    }
    Solution 3 (The most optimised one):
    Provided in this video :)

  • @cdrrjt5005
    @cdrrjt5005 2 ปีที่แล้ว

    First comment😊

  • @avadhutlohar5181
    @avadhutlohar5181 2 ปีที่แล้ว

    #Day_10
    Worst Case Time Complexity is O(2^n)
    Space Complexity is O(n)
    Consistency OP 🔥🔥🔥

  • @Raj-pi1pl
    @Raj-pi1pl 2 ปีที่แล้ว

    L-10 completed waitng for L-11

  • @Lucifer-xt7un
    @Lucifer-xt7un 2 ปีที่แล้ว

    Consistency ++