DP 44. Largest Divisible Subset | Longest Increasing Subsequence

Sdílet
Vložit
  • čas přidán 11. 07. 2024
  • Lecture Notes/C++/Java Codes: takeuforward.org/dynamic-prog...
    Problem Link: bit.ly/3rON1Ef
    Please watch the video at 1.25x for a better experience.
    Pre-req for this Series: • Re 1. Introduction to ...
    a
    Make sure to join our telegram group for discussions: linktr.ee/takeUforward
    Full Playlist: • Striver's Dynamic Prog...
    In this video, we solve the Largest divisible Subset, but please make sure to watch DP 41 and DP 42 prior to this video.
    If you have not yet checked our SDE sheet, you should definitely do it: takeuforward.org/interviews/s...
    You can also get in touch with me at my social handles: linktr.ee/takeUforward

Komentáře • 217

  • @your_name96
    @your_name96 Před 2 lety +21

    Thanks man, though the observation was pretty simple, and the bias that I knew LIS was needed for this problem, I was able to do it by myself(though I had to refer to the notes for some of the printing details :P),your first 3 videos of LIS have been phenomenal!
    I hope to solve problems like in unknown environment as well.

  • @sauravchandra10
    @sauravchandra10 Před rokem +1

    Wow, intuitive and simple. Understood clearly, thanks!

  • @Aryan-tg3uu
    @Aryan-tg3uu Před 2 lety +41

    Not yet at these problems, just saw the notification and came to say thank you for your efforts Striver! 🔥👍🏼

  • @stith_pragya
    @stith_pragya Před 6 měsíci +2

    UNDERSTOOD...........Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @gautamgrover1087
    @gautamgrover1087 Před rokem +2

    main good thing with your videos is the depth of every question uh discuss which nobody does in even paid courses

  • @ritikshandilya7075
    @ritikshandilya7075 Před 3 dny

    What an amazing approach , no one assumed LIS would be tweaked such that we can solve LDS in such a simple way . Thankyou so much Striver

  • @tasneemayham974
    @tasneemayham974 Před 8 měsíci

    Crystal Clear Sir!!!

  • @cinime
    @cinime Před rokem +2

    Understood! Amazing!! Thank you very much as always!!

    • @sundeepkumar3871
      @sundeepkumar3871 Před rokem

      hey bro /sis , please see my comment above and solve my doubt please , sort by recent comments and see my comment

  • @Dontpushyour_luck
    @Dontpushyour_luck Před rokem +5

    Immediately after you told that we will sort and use lis idea, I was able to solve this on my own. Thank you striver for making me this capable

  • @UECAshutoshKumar
    @UECAshutoshKumar Před 14 dny +1

    Thank You!
    Understood!!!

  • @ganeshkamath89
    @ganeshkamath89 Před 2 lety +10

    Thanks Striver. Understood.
    To summarize the changes:
    1) sort the array
    2) change comparison (arr[prev] < arr[i]) to modulo (arr[i] % arr[prev])
    3) return the temp array
    I am summarizing because I forgot to sort the array when I tried and later realized that change.

    • @your_name96
      @your_name96 Před 2 lety +4

      I guess there is a thumb rule, in case of subset type of probs, never hesitate to sort

  • @chanchalroy3417
    @chanchalroy3417 Před 5 měsíci

    Understood totally 💥😌

  • @DevashishJose
    @DevashishJose Před 5 měsíci

    understood thank you so much.

  • @disunique6107
    @disunique6107 Před 2 lety

    Understoooood the intuition ......... thanks 😊

  • @kms8320
    @kms8320 Před 2 lety +1

    nice question and completely understood

  • @ratinderpalsingh5909
    @ratinderpalsingh5909 Před 11 měsíci

    Understood, sir. Thank you very much.

  • @rohanrastogi1077
    @rohanrastogi1077 Před 2 lety +26

    no need to reverse temp as we have to find subset ,not subsequence

  • @adebisisheriff159
    @adebisisheriff159 Před 6 měsíci

    Thanks Striver

  • @gauravtiwari6104
    @gauravtiwari6104 Před 8 dny

    understood striver bhai

  • @deepaliaggarwal6429
    @deepaliaggarwal6429 Před 2 lety +5

    Hey Thanks for this series striver🥰!! I just have a small doubt, I know this rule that if a/b and b/c that means a/c also. Do we need this condition to be also satisfied that a < b < c ? Can you once confirm? I know this is very basic maths but still🙂

    • @takeUforward
      @takeUforward  Před 2 lety

      Yes

    • @ankishkhandelwal1061
      @ankishkhandelwal1061 Před rokem

      i think because of these we sort arr ????

    • @sundeepkumar3871
      @sundeepkumar3871 Před rokem

      @@takeUforward hey bro, please see my comment above and solve my doubt please , sort by recent comments and see my comment

    • @sundeepkumar3871
      @sundeepkumar3871 Před rokem

      hey sis , please see my comment above and solve my doubt please , sort by recent comments and see my comment

  • @harshitsen5479
    @harshitsen5479 Před 9 měsíci +1

    Recursion solution, kind of same as combination sum - 1.
    // Array is sorted and then recursion function is called.
    private static void recursion(int idx, int prevIdx, int[] arr, List list, List output, int n) {
    if(idx == n) {
    if(list.size() > output.size()) {
    output.clear();
    output.addAll(list);
    }
    return;
    }
    if(prevIdx == -1 || arr[idx] % arr[prevIdx] == 0) {
    list.add(arr[idx]);
    recursion(idx + 1, idx, arr, list, output, n);
    list.remove(list.size()-1);
    }
    recursion(idx+1, prevIdx, arr, list, output, n);
    }

  • @yeswanthh5068
    @yeswanthh5068 Před rokem +1

    Understood thank u very much

  • @sameersahu4569
    @sameersahu4569 Před 2 lety

    Understood!!Thank you

  • @rishabhagarwal8049
    @rishabhagarwal8049 Před rokem

    Understood Sir, Thank you very much

  • @earningonlinevip8147
    @earningonlinevip8147 Před rokem

    very easy beacuse you explain very smartly

  • @arihantjammar8888
    @arihantjammar8888 Před 8 měsíci

    Understood 😊

  • @aj9706
    @aj9706 Před 2 lety +17

    Thanks!

  • @sangammishra3670
    @sangammishra3670 Před rokem

    why we nened to sort it cant it be like every time check with previous only and then add in the set

  • @prabhakaran5542
    @prabhakaran5542 Před 2 měsíci +1

    Understood ❤

  • @Harshit126
    @Harshit126 Před rokem

    Understood, thanks

  • @chiragbansod8252
    @chiragbansod8252 Před 5 měsíci

    UNDERSTOOD

  • @lakshaysharma6550
    @lakshaysharma6550 Před 2 lety

    UNDERSTOOD!!!!🔥🔥🔥🔥

  • @akshaali28
    @akshaali28 Před 11 měsíci

    Loved the content!

  • @pragyanandsingh6673
    @pragyanandsingh6673 Před 4 měsíci

    Understood !!

  • @user-rf3he9uv5m
    @user-rf3he9uv5m Před 9 měsíci

    understood

  • @ujjalsaha428
    @ujjalsaha428 Před 2 lety +1

    As always "understood" ❤️

  • @studynewthings1727
    @studynewthings1727 Před 2 měsíci +1

    Understood.

  • @premduvvapu476
    @premduvvapu476 Před 9 měsíci

    Understood

  • @stl8857
    @stl8857 Před rokem

    understood and cant we solve it like this:
    traverse through the sorted array and for each element just run a loop until log(max/a[i]) and check if a[i]*pow(2,j) is present or not using map or anything it will take nlogn

  • @jayagarwal9759
    @jayagarwal9759 Před 2 lety

    understood!

  • @srinathv1412
    @srinathv1412 Před 5 měsíci

    Understood !!!!

  • @vaishnavithakur6460
    @vaishnavithakur6460 Před 2 lety +1

    understood!!!

  • @nigam_sharma
    @nigam_sharma Před 5 měsíci +1

    🎯 Key Takeaways for quick navigation:
    00:13 🧠 *Understand the importance of understanding longest increasing subsequence before tackling the largest divisible subset problem.*
    03:37 🎯 *To solve the largest divisible subset problem, grasp the concepts of subset, divisibility, and finding the largest possible subset.*
    05:55 💡 *By sorting the array, you can simplify the process of identifying divisible pairs within the subset.*
    08:24 🔄 *Transform the problem of finding the longest divisible subsequence into a variation of the longest increasing subsequence.*
    11:29 🛠️ *Key insight: Sort the array to ensure divisibility relationships between elements, crucial for the solution logic to work.*
    13:57 ⏰ *Time complexity of the solution is O(n^2) due to nested loops, with an additional O(n) for tracking back the longest subset.*
    Made with HARPA AI

  • @rishabhgupta9846
    @rishabhgupta9846 Před rokem

    understood,able to solve by own

  • @JoySaha-jg1qi
    @JoySaha-jg1qi Před 10 měsíci

    Thank you for amaging contents.

  • @user-rm7jm5tl3w
    @user-rm7jm5tl3w Před dnem

    time complexity: O(n2)+O(nlogn)+O(n)

  • @m-bk4um
    @m-bk4um Před 7 měsíci

    understand

  • @sujalgupta6100
    @sujalgupta6100 Před rokem

    Ye last wala code nahi chal rha mere mei, Leetcode pe,
    jo code blog mei diya hai usme last for loop undo nested loops ke bahar hai wo chal rha hai

  • @_hulk748
    @_hulk748 Před rokem +1

    understood sir🙏❤

  • @utkrishtsingh5520
    @utkrishtsingh5520 Před 2 lety +1

    Understood

  • @srikarvinjamara2003
    @srikarvinjamara2003 Před 5 měsíci

    Hi!. Could any one please tell me how do i think of sorting the array? I knew this had to be done in some way similar to the LIS problem but it did not come to my mind that i need to sort it. Why does it work when sorted? Please and Thank You!

    • @sahulraj9536
      @sahulraj9536 Před 5 měsíci

      since they asked for subset ,the order of the elements does not matter so we can sort them
      if they asked a longest divisible subsequence they we cant do sorting.in fact while solving i assumed subset as subsequence and tried it without sorting and i didnt get the right answer

  • @amansamal8233
    @amansamal8233 Před 2 lety

    understood❤❤

  • @sayantanacharya2040
    @sayantanacharya2040 Před rokem

    Hey man!Really enjoying your vidies... can u please tell me if the java code is available yet?Would be really helpful..."US"..

  • @giraffe4375
    @giraffe4375 Před 2 lety

    understood striver

  • @anujbajpai5791
    @anujbajpai5791 Před 9 měsíci

    If we are still traversing all the previous indexes for each index then why do we need to sort it? If 16 is before 8, and count at 16 is 2 then for 8 it will be 3 irrespective the order of occurence. Can someone explain where am I missing?

    • @LearningYouth
      @LearningYouth Před 9 měsíci +2

      Let's say we have [16 1 18]
      count will be : [ 1 2 3 ] which indicates, taking last element as 18, max length of LDS can be formed 3 which is incorrect.
      Bcz max length of LDS is 2. Either [16 1] or [1 18]
      So you need to sort otherwise it'll give you WA.

  • @tikshavarshney213
    @tikshavarshney213 Před 2 lety

    Understood !

  • @krishanpratap3286
    @krishanpratap3286 Před 2 lety +2

    isme agar sroted nahi bhi ho then also if we add in they array jisme hum store kar rahe den also who logic apply ho jayega nah pls ek baar batana

    • @disunique6107
      @disunique6107 Před 2 lety

      No then it will not give correct subset... As (16,1,8 ) != (1,8,16)
      1%16 will give remainder =1 ;
      8%1 will give 0..
      But in sorted 8%1 ==0 and also 16%8 ==0

  • @iamnoob7593
    @iamnoob7593 Před 6 měsíci

    US striver Thanks

  • @sangharshpipare666
    @sangharshpipare666 Před 22 dny

    What is the importance of initialising the hash array to the respective i values ? Since we will have the lastIndex we will just be jumping to the correct indexes of hash array. Without it the solution is failing, any help is appreciated
    The Test Case: arr = {20, 19, 11, 25, 21}
    Ok, realised what was wrong if we didn't initialise the hash array and if no elements are exactly divisible the hash array will have all elements 0. But the while loop will execute one time before terminating and we get a wrong answer

  • @jinishshah627
    @jinishshah627 Před 2 lety

    UNDERSTTOOOOOOOOOOODDDDDDDDDDDDDDDDDDDD!

  • @ITArchanaGK
    @ITArchanaGK Před rokem

    understood✨

  • @rohalkurup1350
    @rohalkurup1350 Před 2 lety

    Understood !!!!!!

  • @subhajitdey135
    @subhajitdey135 Před 10 dny

    Thought process of sorting: If the biggest numbers gets divided by the incoming greater number or the vice-versa, then all other smaller elements in the array will be divisible.

  • @adityasaxena6971
    @adityasaxena6971 Před rokem +1

    Understood Striver💯💯

  • @original_gangsta_
    @original_gangsta_ Před 2 lety +1

    Understood 💯💯💯

  • @SNX03
    @SNX03 Před rokem

    This could be done in O(nlogn) also by simultaneously creating parent array during binary searching the upper_bound

    • @shivamkumar5857
      @shivamkumar5857 Před rokem +1

      how do u print it

    • @vikramket
      @vikramket Před 9 měsíci

      ​@@shivamkumar5857yes its not possible to print it

  • @parshchoradia9909
    @parshchoradia9909 Před rokem

    Understood Sir!

  • @harshitgoyal9341
    @harshitgoyal9341 Před rokem

    No need to reverse the temp vector in end ................. as it has asked for subset in any order

  • @dhairyachauhan6622
    @dhairyachauhan6622 Před rokem +2

    Recursive sol for those who were trying to solve like me ..... (C++)
    #include
    void solve(vector&arr,int n,int idx,int prev_idx,vector&ans,vector&output){
    if(idx == n){
    if(output.size()>ans.size()){
    ans = output;
    }
    return;
    }
    if(prev_idx == -1 || arr[idx]%prev_idx==0){
    solve(arr,n,idx+1,prev_idx,ans,output);
    output.push_back(arr[idx]);
    solve(arr,n,idx+1,arr[idx],ans,output);
    output.pop_back();
    }
    else{
    solve(arr,n,idx+1,prev_idx,ans,output);
    }
    }
    vector divisibleSet(vector &arr){
    sort(arr.begin(),arr.end());
    vectorans;
    vectoroutput;
    int n = arr.size();
    solve(arr,n,0,-1,ans,output);
    return ans;
    }

  • @asm9450
    @asm9450 Před 2 lety

    In the if loop (line no.: 10) at 12:24 can we omit the second condition of the if loop (i.e. 1+dp[prev]>dp[i] )?
    Btw amazing explanation Understood

    • @rohankumarshah5679
      @rohankumarshah5679 Před 2 lety

      idea is to find largest such subset. that means the length should be more

    • @gurmeetsingh2016
      @gurmeetsingh2016 Před 2 lety

      Yes also we if the condition satisifies arr[ind]%arr[j]==0 so dp[ind]=dp[j]+1 and hash[ind]=j and break
      There is no need to check others so break out of the loop after that

  • @aditithakur6226
    @aditithakur6226 Před rokem

    Understood Sir.

  • @VipinKumar-us1sr
    @VipinKumar-us1sr Před 2 lety +2

    Understood. Actually, we can do it without using hash array. By traversing the dp array and starting from the index which have max value and then moving to the left and checking if ( dp[i] == dp[mx] + 1 and arr[mx]%arr[i] == 0 ) then do { ans.push_back(arr[i]); mx = i; i--; }

    • @vishalgupta957
      @vishalgupta957 Před 2 lety +2

      i think we can't because there can be duplicate values in dp and we don't know which indices includes our answer.

    • @sundeepkumar3871
      @sundeepkumar3871 Před rokem

      hey bro /sis , please see my comment above and solve my doubt please , sort by recent comments and see my comment

    • @rishabhgupta9846
      @rishabhgupta9846 Před rokem +1

      no we can't may be its value is equal but not divides

    • @ashutoshpandey1639
      @ashutoshpandey1639 Před 9 měsíci

      Yes , we can follow this approach too

  • @googleit2490
    @googleit2490 Před 7 měsíci

    DP Revision:
    Done and dusted in DP revision :)
    (11 mins, exact code without watching the video earlier any before)
    Nov'20, 2023 03:45 pm

    • @jayrathod7957
      @jayrathod7957 Před 6 měsíci

      bhaai kya baat he,...............
      me bhi try karta hu lekin mujse nahi ho raha kuchhh tip de sakte he ky??

    • @googleit2490
      @googleit2490 Před 6 měsíci

      @@jayrathod7957 Kuchh logo ko kam time lagta h, kuchh ko maybe jyada lag skta h. But, believe me, if you are consistent enough, you will crack it one day.

  • @venkateshvenky2880
    @venkateshvenky2880 Před 2 lety +1

    #understood

  • @ganeshmula4508
    @ganeshmula4508 Před rokem

    understood sir

  • @user-tg9kx3ze5p
    @user-tg9kx3ze5p Před 6 měsíci

    no need to reverse at the end since we can print in any order

  • @Superheroic_Anime
    @Superheroic_Anime Před 2 lety

    Understood bhaiya !!!

  • @VikashYadav-px8ei
    @VikashYadav-px8ei Před rokem +1

    Understood 🎉

  • @enigma2777
    @enigma2777 Před rokem +1

    Can someone please share the recursive code for this problem

    • @HimanshuGupta-ni3pk
      @HimanshuGupta-ni3pk Před rokem

      have you got ?

    • @riyakumari8377
      @riyakumari8377 Před rokem

      def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
      n = len(nums)
      nums.sort()
      ans = []
      def recur(i, path):
      nonlocal ans
      if i == n:
      ans.append(path)
      return ans
      notTake = recur(i+1, path)
      if (((len(path) > 0 and (path[-1] % nums[i] == 0 or nums[i] % path[-1] == 0)) or len(path) == 0)) and nums[i] not in path:
      # print(path, nums[i])
      take = recur(i+1, path + [nums[i]])
      return
      recur(0, [])
      res = []
      # print(ans)
      for arr in ans:
      if len(arr) > len(res):
      res = arr
      # print(res)
      return res
      Though this will give u TLE!

  • @codewithom11
    @codewithom11 Před 10 měsíci

    Understood🙂🙃

  • @manikiran949
    @manikiran949 Před 4 měsíci

    Understood++

  • @imilanprj
    @imilanprj Před 2 lety +2

    🔥

  • @RohitKumar-dy2gc
    @RohitKumar-dy2gc Před 7 měsíci

    US

  • @HeroHunter07
    @HeroHunter07 Před 5 měsíci +1

    Today's question

  • @manasranjanmahapatra3729

    UnderStood💥

  • @harshbardhanon4901
    @harshbardhanon4901 Před rokem +1

    Love from Pak🙂🕌

  • @priyanshvatsal9791
    @priyanshvatsal9791 Před rokem

    Understood🥰

  • @mriduljain6809
    @mriduljain6809 Před rokem

    Understood😀😀

  • @splinkerp2222
    @splinkerp2222 Před 2 lety +1

    You are legend bhayya

  • @arkasheikh3539
    @arkasheikh3539 Před rokem

    "UNDERSTOOD"

  • @preetkatiyar969
    @preetkatiyar969 Před rokem

    Sir there is no java code in notes

  • @addityasharma6426
    @addityasharma6426 Před 2 lety

    understood:-)

  • @rayyansyed2998
    @rayyansyed2998 Před 9 měsíci

    "us"

  • @codingp110
    @codingp110 Před měsícem

    US!

  • @Skm-mq1sw
    @Skm-mq1sw Před 2 lety +1

    US Bhaiya

  • @priyanshuchandrakar172
    @priyanshuchandrakar172 Před 8 měsíci

    us

  • @sarangr8624
    @sarangr8624 Před 2 lety

    Us

  • @namanjain1684
    @namanjain1684 Před rokem

    please submit it in leetcode

  • @keertilata20
    @keertilata20 Před 2 lety +1

    You forgot the additional time complexity for sorting the array

  • @arnabroy2995
    @arnabroy2995 Před rokem

    US❤