DP 44. Largest Divisible Subset | Longest Increasing Subsequence

  Рет қаралды 109,990

take U forward

take U forward

Күн бұрын

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

Пікірлер: 221
@akshayaashok8794
@akshayaashok8794 10 күн бұрын
This solution was really unexpected!! Nice to see that code for printing Longest increasing subsequence can be tweaked to solve longest divisible subsequence if we sort the array. Thank you so much!!! Understood.
@your_name96
@your_name96 2 жыл бұрын
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.
@ritikshandilya7075
@ritikshandilya7075 22 күн бұрын
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
@priyanshkumariitd
@priyanshkumariitd 21 күн бұрын
yeah
@Aryan-tg3uu
@Aryan-tg3uu 2 жыл бұрын
Not yet at these problems, just saw the notification and came to say thank you for your efforts Striver! 🔥👍🏼
@sauravchandra10
@sauravchandra10 Жыл бұрын
Wow, intuitive and simple. Understood clearly, thanks!
@gautamgrover1087
@gautamgrover1087 Жыл бұрын
main good thing with your videos is the depth of every question uh discuss which nobody does in even paid courses
@stith_pragya
@stith_pragya 6 ай бұрын
UNDERSTOOD...........Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@ganeshkamath89
@ganeshkamath89 2 жыл бұрын
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 2 жыл бұрын
I guess there is a thumb rule, in case of subset type of probs, never hesitate to sort
@Dontpushyour_luck
@Dontpushyour_luck Жыл бұрын
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
@nigam_sharma
@nigam_sharma 5 ай бұрын
🎯 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
@cinime
@cinime 2 жыл бұрын
Understood! Amazing!! Thank you very much as always!!
@sundeepkumar3871
@sundeepkumar3871 Жыл бұрын
hey bro /sis , please see my comment above and solve my doubt please , sort by recent comments and see my comment
@tasneemayham974
@tasneemayham974 9 ай бұрын
Crystal Clear Sir!!!
@rohanrastogi1077
@rohanrastogi1077 2 жыл бұрын
no need to reverse temp as we have to find subset ,not subsequence
@siddhaarthsingh7414
@siddhaarthsingh7414 2 жыл бұрын
Mera TLE aara h Can y share ur code
@krishnavamsichinnapareddy
@krishnavamsichinnapareddy 2 жыл бұрын
Yeah 👍
@kainathussain6973
@kainathussain6973 2 жыл бұрын
class Solution { public: vector largestDivisibleSubset(vector& a) { int n = a.size(); sort(a.begin() , a.end()); vector dp(n,1) , prev(n); for(int i= 0;i
@aj9706
@aj9706 2 жыл бұрын
Thanks!
@UECAshutoshKumar
@UECAshutoshKumar Ай бұрын
Thank You! Understood!!!
@harshitsen5479
@harshitsen5479 10 ай бұрын
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); }
@DevashishJose
@DevashishJose 6 ай бұрын
understood thank you so much.
@chanchalroy3417
@chanchalroy3417 6 ай бұрын
Understood totally 💥😌
@kms8320
@kms8320 2 жыл бұрын
nice question and completely understood
@disunique6107
@disunique6107 2 жыл бұрын
Understoooood the intuition ......... thanks 😊
@gauravtiwari6104
@gauravtiwari6104 27 күн бұрын
understood striver bhai
@ratinderpalsingh5909
@ratinderpalsingh5909 Жыл бұрын
Understood, sir. Thank you very much.
@stl8857
@stl8857 Жыл бұрын
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
@deepaliaggarwal6429
@deepaliaggarwal6429 2 жыл бұрын
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 2 жыл бұрын
Yes
@ankishkhandelwal1061
@ankishkhandelwal1061 Жыл бұрын
i think because of these we sort arr ????
@sundeepkumar3871
@sundeepkumar3871 Жыл бұрын
@@takeUforward hey bro, please see my comment above and solve my doubt please , sort by recent comments and see my comment
@sundeepkumar3871
@sundeepkumar3871 Жыл бұрын
hey sis , please see my comment above and solve my doubt please , sort by recent comments and see my comment
@adebisisheriff159
@adebisisheriff159 6 ай бұрын
Thanks Striver
@prabhakaran5542
@prabhakaran5542 2 ай бұрын
Understood ❤
@earningonlinevip8147
@earningonlinevip8147 Жыл бұрын
very easy beacuse you explain very smartly
@sangammishra3670
@sangammishra3670 2 жыл бұрын
why we nened to sort it cant it be like every time check with previous only and then add in the set
@rishabhagarwal8049
@rishabhagarwal8049 Жыл бұрын
Understood Sir, Thank you very much
@studynewthings1727
@studynewthings1727 3 ай бұрын
Understood.
@yeswanthh5068
@yeswanthh5068 Жыл бұрын
Understood thank u very much
@arihantjammar8888
@arihantjammar8888 9 ай бұрын
Understood 😊
@sameersahu4569
@sameersahu4569 2 жыл бұрын
Understood!!Thank you
@chiragbansod8252
@chiragbansod8252 5 ай бұрын
UNDERSTOOD
@akshaali28
@akshaali28 Жыл бұрын
Loved the content!
@user-nb1ye5tf1r
@user-nb1ye5tf1r 11 күн бұрын
I read comments, and i was able to solve this problem.
@pragyanandsingh6673
@pragyanandsingh6673 5 ай бұрын
Understood !!
@abhinanda7049
@abhinanda7049 4 ай бұрын
understood
@Harshit126
@Harshit126 2 жыл бұрын
Understood, thanks
@shreeshdivyamsinha126
@shreeshdivyamsinha126 2 ай бұрын
Understood
@lakshaysharma6550
@lakshaysharma6550 2 жыл бұрын
UNDERSTOOD!!!!🔥🔥🔥🔥
@srinathv1412
@srinathv1412 6 ай бұрын
Understood !!!!
@user-rm7jm5tl3w
@user-rm7jm5tl3w 21 күн бұрын
time complexity: O(n2)+O(nlogn)+O(n)
@ujjalsaha428
@ujjalsaha428 2 жыл бұрын
As always "understood" ❤️
@anujbajpai5791
@anujbajpai5791 10 ай бұрын
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 10 ай бұрын
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.
@sayantanacharya2040
@sayantanacharya2040 Жыл бұрын
Hey man!Really enjoying your vidies... can u please tell me if the java code is available yet?Would be really helpful..."US"..
@vaishnavithakur6460
@vaishnavithakur6460 2 жыл бұрын
understood!!!
@sujalgupta6100
@sujalgupta6100 Жыл бұрын
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
@jayagarwal9759
@jayagarwal9759 2 жыл бұрын
understood!
@subhajitdey135
@subhajitdey135 29 күн бұрын
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.
@iamnoob7593
@iamnoob7593 6 ай бұрын
US striver Thanks
@m-bk4um
@m-bk4um 8 ай бұрын
understand
@harshitgoyal9341
@harshitgoyal9341 Жыл бұрын
No need to reverse the temp vector in end ................. as it has asked for subset in any order
@SNX03
@SNX03 Жыл бұрын
This could be done in O(nlogn) also by simultaneously creating parent array during binary searching the upper_bound
@shivamkumar5857
@shivamkumar5857 Жыл бұрын
how do u print it
@vikramket
@vikramket 9 ай бұрын
​@@shivamkumar5857yes its not possible to print it
@krishanpratap3286
@krishanpratap3286 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 2 жыл бұрын
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
@rishabhgupta9846
@rishabhgupta9846 Жыл бұрын
understood,able to solve by own
@JoySaha-jg1qi
@JoySaha-jg1qi 11 ай бұрын
Thank you for amaging contents.
@sangharshpipare666
@sangharshpipare666 Ай бұрын
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
@srikarvinjamara2003
@srikarvinjamara2003 6 ай бұрын
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 5 ай бұрын
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
@asm9450
@asm9450 2 жыл бұрын
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 2 жыл бұрын
idea is to find largest such subset. that means the length should be more
@gurmeetsingh2016
@gurmeetsingh2016 2 жыл бұрын
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
@utkrishtsingh5520
@utkrishtsingh5520 2 жыл бұрын
Understood
@user-tg9kx3ze5p
@user-tg9kx3ze5p 6 ай бұрын
no need to reverse at the end since we can print in any order
@giraffe4375
@giraffe4375 2 жыл бұрын
understood striver
@_hulk748
@_hulk748 Жыл бұрын
understood sir🙏❤
@dhairyachauhan6622
@dhairyachauhan6622 Жыл бұрын
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; }
@suryakiran2970
@suryakiran2970 Жыл бұрын
Is is gives TLE, because it is not Memoize ?
@adityasaxena6971
@adityasaxena6971 Жыл бұрын
Understood Striver💯💯
@tikshavarshney213
@tikshavarshney213 2 жыл бұрын
Understood !
@jinishshah627
@jinishshah627 2 жыл бұрын
UNDERSTTOOOOOOOOOOODDDDDDDDDDDDDDDDDDDD!
@VipinKumar-us1sr
@VipinKumar-us1sr 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 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 Жыл бұрын
hey bro /sis , please see my comment above and solve my doubt please , sort by recent comments and see my comment
@rishabhgupta9846
@rishabhgupta9846 Жыл бұрын
no we can't may be its value is equal but not divides
@ashutoshpandey1639
@ashutoshpandey1639 10 ай бұрын
Yes , we can follow this approach too
@amansamal8233
@amansamal8233 2 жыл бұрын
understood❤❤
@rohalkurup1350
@rohalkurup1350 2 жыл бұрын
Understood !!!!!!
@googleit2490
@googleit2490 8 ай бұрын
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 6 ай бұрын
bhaai kya baat he,............... me bhi try karta hu lekin mujse nahi ho raha kuchhh tip de sakte he ky??
@googleit2490
@googleit2490 6 ай бұрын
@@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.
@parshchoradia9909
@parshchoradia9909 Жыл бұрын
Understood Sir!
@ITArchanaGK
@ITArchanaGK Жыл бұрын
understood✨
@original_gangsta_
@original_gangsta_ 2 жыл бұрын
Understood 💯💯💯
@aditithakur6226
@aditithakur6226 Жыл бұрын
Understood Sir.
@manikiran949
@manikiran949 5 ай бұрын
Understood++
@ganeshmula4508
@ganeshmula4508 Жыл бұрын
understood sir
@Superheroic_Anime
@Superheroic_Anime 2 жыл бұрын
Understood bhaiya !!!
@VikashYadav-px8ei
@VikashYadav-px8ei Жыл бұрын
Understood 🎉
@venkateshvenky2880
@venkateshvenky2880 2 жыл бұрын
#understood
@enigma2777
@enigma2777 Жыл бұрын
Can someone please share the recursive code for this problem
@HimanshuGupta-ni3pk
@HimanshuGupta-ni3pk Жыл бұрын
have you got ?
@riyakumari8377
@riyakumari8377 Жыл бұрын
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!
@HeroHunter07
@HeroHunter07 5 ай бұрын
Today's question
@codewithom11
@codewithom11 11 ай бұрын
Understood🙂🙃
@codingp110
@codingp110 2 ай бұрын
US!
@RohitKumar-dy2gc
@RohitKumar-dy2gc 8 ай бұрын
US
@harshbardhanon4901
@harshbardhanon4901 Жыл бұрын
Love from Pak🙂🕌
@preetkatiyar969
@preetkatiyar969 Жыл бұрын
Sir there is no java code in notes
@manasranjanmahapatra3729
@manasranjanmahapatra3729 Жыл бұрын
UnderStood💥
@imilanprj
@imilanprj 2 жыл бұрын
🔥
@mriduljain6809
@mriduljain6809 Жыл бұрын
Understood😀😀
@priyanshvatsal9791
@priyanshvatsal9791 Жыл бұрын
Understood🥰
@rayyansyed2998
@rayyansyed2998 10 ай бұрын
"us"
@arkasheikh3539
@arkasheikh3539 Жыл бұрын
"UNDERSTOOD"
@splinkerp2222
@splinkerp2222 2 жыл бұрын
You are legend bhayya
@ojasdighe991
@ojasdighe991 2 жыл бұрын
Thanks bro
@Skm-mq1sw
@Skm-mq1sw 2 жыл бұрын
US Bhaiya
@keertilata20
@keertilata20 2 жыл бұрын
You forgot the additional time complexity for sorting the array
@dolbyagarwal9316
@dolbyagarwal9316 2 жыл бұрын
additional but still N^2 is greater than NlogN
@keertilata20
@keertilata20 2 жыл бұрын
@@dolbyagarwal9316 yess
@priyanshuchandrakar172
@priyanshuchandrakar172 8 ай бұрын
us
@addityasharma6426
@addityasharma6426 2 жыл бұрын
understood:-)
@arnabroy2995
@arnabroy2995 Жыл бұрын
US❤
DP 45. Longest String Chain | Longest Increasing Subsequence | LIS
16:57
The Longest Increasing Subsequence
16:59
Spectral Collective
Рет қаралды 52 М.
Looks realistic #tiktok
00:22
Анастасия Тарасова
Рет қаралды 106 МЛН
Heartwarming Unity at School Event #shorts
00:19
Fabiosa Stories
Рет қаралды 23 МЛН
Largest Divisible Subset - Leetcode 368 - Python
22:57
NeetCodeIO
Рет қаралды 15 М.
The High Schooler Who Solved a Prime Number Theorem
5:15
Quanta Magazine
Рет қаралды 2,2 МЛН
Nature's Incredible ROTATING MOTOR (It’s Electric!) - Smarter Every Day 300
29:37
The Backtracking Blueprint: The Legendary 3 Keys To Backtracking Algorithms
13:44
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 630 М.
the beauty of prime numbers in cryptography
4:36
Jeffrey Ventrella
Рет қаралды 12 М.