Finding Maximum Sum SubArray using Divide and Conquer Approach.

  Рет қаралды 140,976

Ashish Kumar

Ashish Kumar

4 жыл бұрын

Algorithms Playlist: • Algorithms with Practi...
Placement Preparation Playlist: • Placement Preparation ...
Competitive Programming Playlist: • Competitive Programmin...
Graph Theory Playlist: • Graph Theory Complete
Subscribe for more DSA videos!
**Resources for Competitive Programming/Coding Interviews:
Competitive Programming Course:tinyurl.com/BibleOfCP
DS Algo Course:tinyurl.com/AbdulBariDSA
DS Algo and Coding Interview Course:tinyurl.com/DSACodingInterview
Dynamic Programming:tinyurl.com/DynamicProgrammin...
Competitive Programming Essentials:tinyurl.com/CPEssential
**
Edit: I made a mistake for the subarray on the left [-2,-5,6,-2]: The LSS=-2 RSS=6 and CSS=1.
For CSS{ mid+1=6 ,to the right max=6},{mid=-5 ,on the left max=-5} so CSS=(6-5)=1 Not -1.
See the code for more idea of CSS: • LeetCode 53 Maximum S...
In this video I am going to show how to use divide and conquer to find the maximum value of sum of a contiguous subarray.
For the Code: • LeetCode 53 Maximum S...

Пікірлер: 87
@ashishcode
@ashishcode 7 ай бұрын
Hi Guys, you can now book a 1:1 call with me on topmate.io/ashishcode for placement guidance and more at a very affordable price. Use code KZfaq30 for 30% off! (Only for first 15 Users)
@sareek007
@sareek007 2 жыл бұрын
for css if anybody is confused, have a look at calculating CSS for below, -2,-5,6,-2,-3,1,5,-6 divide into 2 we get, -2,-5,6,-2 .....................,-3,1,5,-6 for left part, go from right to left, (-2) + (6) = 4 4 + (-5) = -1 -1 + (-2) = -3 Now maximum of all these results is 4, similarly for right part i.e. -3,1,5,-6 do same go from left to right starting from middle(i.e. ......) adding two consecutive elements, we get, (-3) + 1 = -2 -2 + (5) = 3 3 + (-6) = -3 what is the maximum of these three ?? it is 3, right? so from left we have maximum value 4 and from right 3, add these two to get CSS value, that is 7
@rishitaraha1204
@rishitaraha1204 2 жыл бұрын
but when we are going left in (-2, -5, 6, -2) from mid, we are taking max( -5 + -2, -2) right? So why aren't we doing the same for the right part? Why not max(6, 6-2)?
@mohitchintu9522
@mohitchintu9522 2 жыл бұрын
I think it should be 6
@LalitSingh-sr4lo
@LalitSingh-sr4lo Жыл бұрын
@@mohitchintu9522 no it cannot be 6 ...we have to look for a subarray not for any subsequence.
@ShivamSingh-gt5vx
@ShivamSingh-gt5vx Жыл бұрын
but when we are going left in (-2, -5, 6, -2) the answer in video is -1 for css and you are saying 4
@sonit9707
@sonit9707 Жыл бұрын
@@ShivamSingh-gt5vx It is CSS for the full array, not the left part.
@shreemantolahiri800
@shreemantolahiri800 11 ай бұрын
At @5:11 the CSS should be +1, because: from left, A=-5 from right, B=6 not 6-2=4(wrong) so A+B=-5+6=1 CSS must be +1. Spent 20 mins thinking was I wrong, or @AshishKumar. Nevertheless, ans will be same. Please correct me iif I'm wrong. Hope this helps someone like me xD
@Brana_records
@Brana_records 3 ай бұрын
Thank you!
@user-sk6eg2rc6u
@user-sk6eg2rc6u 2 ай бұрын
thank you soo much
@devanshmesson2777
@devanshmesson2777 3 жыл бұрын
If Anyone is still confused regarding explanation of CSS i am writing here : Let A = Maximum continuous sum in left part of the array starting from mid element. Let B = Maximum continuous sum in right part of the array starting from mid+1 'th element. let low = first element of the array let high= last element of the array. So, CSS for an array is calculated as: A + B Q - why are we starting from the mid'th element ? Ans - because CSS means the centre of the array which eventually means that some elements of subarray having maximum sum are in the left part of array and some elements of subarray having maximum sum are in the right part of the array. Suppose, If we compute maximum continuous sum in the order from low to mid (or high to mid+1) then suppose if first element is the subarray having maximum sum, then this method is wrong because this subarray will not be linked to the subarray having maximum sum in the right part of the array. In short we have to link both maximum subarray in left part as well as right part to make it crossing sum subarray(CSS). For example array is 7, -6, 5, 4, -2, 1 (Considering 0 based indexing) index of mid element = 2 value of mid element = 5 A will computed as : Keep adding every element one by one starting from mid element :and keep track of the maximum sum Sum=0 Sum= sum + 5 , , sum=5 , A = 5 Sum = Sum + (-6) , sum=-1, A =5 Sum = Sum + 7 , sum=6 , A = 6 Subarray having maximum continuous sum in left part of the array : [7, -6, 5] B will computed as : Keep adding every element one by one starting from mid element :and keep track of the maximum sum Sum=0 Sum= sum + 4, , sum=4, B = 4 Sum = Sum + (-2) , sum=2, B =4 Sum = Sum + 7 , sum=3 , B = 4 Subarray having maximum continuous sum in right part of the array : [4] SO, CSS for the array [7, -6, 5, 4, -2, 1] = [7, -6, 5, 4] Note : if you start the counting of the sum from the right most part of the array (r) then value of B will be less. Demonstration of the above argument: Sum=0 Sum= sum + 1, , sum=1, B = 1 Sum = Sum + (-2) , sum=-1, B =1 Sum = Sum + 4 , sum=3 , B = 3 So, this proves that CSS should be calculated from the mid element.
@AkashSharma-ij4sn
@AkashSharma-ij4sn 3 жыл бұрын
Nicely explained bro!!
@movocode
@movocode Жыл бұрын
Thank you
@malavikamanoj6802
@malavikamanoj6802 Жыл бұрын
Thank you! :)
@techybhoot
@techybhoot Жыл бұрын
@@malavikamanoj6802 nice explanation bro
@timbenton450
@timbenton450 3 жыл бұрын
Thanks for diagramming this as a tree! I was looking for an explanation that helped me understand how the left and right recursive calls worked since only the crossing function iterates through the array. This explained it perfectly!
@lincolnyousef6973
@lincolnyousef6973 2 жыл бұрын
I realize it is kind of randomly asking but do anybody know a good website to stream new tv shows online ?
@ledgererik2637
@ledgererik2637 2 жыл бұрын
@Lincoln Yousef flixportal :D
@lincolnyousef6973
@lincolnyousef6973 2 жыл бұрын
@Ledger Erik thanks, I went there and it seems like they got a lot of movies there =) I really appreciate it!
@ledgererik2637
@ledgererik2637 2 жыл бұрын
@Lincoln Yousef No problem :)
@user-ik6hy6cy3x
@user-ik6hy6cy3x 3 ай бұрын
wow this conceptual understanding is what i was looking for this cleared a lot of my clouds!!
@parthsalat
@parthsalat 3 жыл бұрын
Best explanation for Finding Maximum Sum SubArray using Divide and Conquer Approach. Thanks.
@froggyy
@froggyy Жыл бұрын
Exactly
@alexanderlin2022
@alexanderlin2022 9 ай бұрын
wow... this problem really blows my mind, feel that trying to use a way more complicated solution to save not much time. (compare to the O(n) solution.)
@hiteshgupta5092
@hiteshgupta5092 4 жыл бұрын
bhaiya bohot acha saamjaya aapne thank you.
@light-qn2jb
@light-qn2jb Жыл бұрын
Thank you it took me all day yesterday to figure out how it get the max sum from left and right with just the max number but with the graph you draw I can see it now the when they reach the bottom out the crossing sum will do all the summation
@Luna-vk9xy
@Luna-vk9xy 3 жыл бұрын
At 5:12 shouldn't css be 1? Going right from mid point would give max sum as 6 not 4. And going left would give -5 not -7. 6-5 would be 1? Correct me if I'm understanding this wrong
@ashishcode
@ashishcode 3 жыл бұрын
Yeah...I made a mistake there. I have mentioned it in the description.
@Luna-vk9xy
@Luna-vk9xy 3 жыл бұрын
@@ashishcode ah okay. I didn't read the description 😬
@shreemantolahiri800
@shreemantolahiri800 11 ай бұрын
lmaooo just wasted 30 mins on thiss. Thanks for confirming
@anshumankumar6099
@anshumankumar6099 3 жыл бұрын
nice one bro!
@pragyanbansal7122
@pragyanbansal7122 2 ай бұрын
🎯 Key Takeaways for quick navigation: 00:00 *📚 The problem discussed is finding the maximum sum subarray from a given array of integers.* 01:07 *🔍 Divide and conquer approach is suggested as a more efficient solution compared to the naive approach of using nested loops.* 03:50 *🔄 For finding the maximum sum subarray, the array is divided into left, right, and crossing subarrays, and the maximum of these is returned recursively.* 06:45 *📊 Calculation of the crossing subarray sum involves finding the maximum sum towards the left and right from the midpoint and summing them up.* 10:15 *🧮 By comparing the maximum sum subarrays from left, right, and crossing, the overall maximum sum subarray is determined algorithmically.* Made with HARPA AI
@josephferraro6052
@josephferraro6052 4 ай бұрын
How can you code this recursively? Since we are doing CSS differently in different cases I cannot see how to do this. We originally do CSS as the sum of the left and right partitions, and then somehow in the last step we do CSS where we find the max sum of the array in total. If we could do that the whole time then that would just be the O(n) solution and not the divide and conquer. As in, the final way u find CSS is O(n) and this also finds the max sum subarray so you are adding extra steps to the O(n) to make it divide and conquer. But I don't see how this is useful if that is the way to find CSS in the final iteration, there must be a different way to find CSS in the last step. What I'm meaning to say here is that you took the O(n) solution - which was the very last step where you found CSS. And then on top of it you did a part of a divide and conquer solution. So I don't understand what the point of the algorithm is if we must use the O(n) solution as a part of the divide and conquer solution.
@safionweb
@safionweb 3 жыл бұрын
Thanks, very helpful!
@arnavborse5944
@arnavborse5944 4 ай бұрын
Thankyou for teaching this algorithm
@RaiMuhammadArslan786
@RaiMuhammadArslan786 3 жыл бұрын
very good approach!!!
@varunkiran3888
@varunkiran3888 3 жыл бұрын
What if there are an odd no of elements?
@kushalljain7887
@kushalljain7887 Ай бұрын
what about odd number of elements , i am getting confused over that
@devanshmesson2777
@devanshmesson2777 3 жыл бұрын
Thank you so much. Best explanation ever!
@jasonbrody468
@jasonbrody468 3 жыл бұрын
Thank you very much 💫
@ashishcode
@ashishcode 3 жыл бұрын
You’re welcome 😊
@nawalali6407
@nawalali6407 3 жыл бұрын
this is amazing
@RohanShetty1992
@RohanShetty1992 3 жыл бұрын
good one buddy
@LightningSpeedtop
@LightningSpeedtop Жыл бұрын
This is awesome
@gudavenkatasaikousik8570
@gudavenkatasaikousik8570 3 ай бұрын
Nice teaching... 👏🏻👏🏻
@gudavenkatasaikousik8570
@gudavenkatasaikousik8570 3 ай бұрын
Don't teach again... 💀💀
@AnshKaurms
@AnshKaurms 3 ай бұрын
@ 5 : 11 you confused me, please tell it clearly.
@sharwansinghrathore3381
@sharwansinghrathore3381 Жыл бұрын
u made me confuse in css part after returning
@anuragdeol9474
@anuragdeol9474 Жыл бұрын
Hey, can anyone please confirm that divide and conquer solution is giving TLE for a very large test case? Or is it just me and have done some mistake in my code? (Leetcode problem - 53. Maximum Subarray)
@bavidlynx3409
@bavidlynx3409 Жыл бұрын
Yes because the optimised algo is dynamic. This algo gives nlogn which is why it gives tle
@priyadharshinig4527
@priyadharshinig4527 3 жыл бұрын
css explanation was lil confusing...
@devanshmesson2777
@devanshmesson2777 3 жыл бұрын
Let A = Maximum continuous sum in left part of the array starting from mid element. Let B = Maximum continuous sum in right part of the array starting from mid+1 'th element. let low = first element of the array let high= last element of the array. So, CSS for an array is calculated as: A + B Q - why are we starting from the mid'th element ? Ans - because CSS means the centre of the array which eventually means that some elements of subarray having maximum sum are in the left part of array and some elements of subarray having maximum sum are in the right part of the array. Suppose, If we compute maximum continuous sum in the order from low to mid (or high to mid+1) then suppose if first element is the subarray having maximum sum, then this method is wrong because this subarray will not be linked to the subarray having maximum sum in the right part of the array. In short we have to link both maximum subarray in left part as well as right part to make it crossing sum subarray(CSS). For example array is 7, -6, 5, 4, -2, 1 (Considering 0 based indexing) index of mid element = 2 value of mid element = 5 A will computed as : Keep adding every element one by one starting from mid element :and keep track of the maximum sum Sum=0 Sum= sum + 5 , , sum=5 , A = 5 Sum = Sum + (-6) , sum=-1, A =5 Sum = Sum + 7 , sum=6 , A = 6 Subarray having maximum continuous sum in left part of the array : [7, -6, 5] B will computed as : Keep adding every element one by one starting from mid element :and keep track of the maximum sum Sum=0 Sum= sum + 4, , sum=4, B = 4 Sum = Sum + (-2) , sum=2, B =4 Sum = Sum + 7 , sum=3 , B = 4 Subarray having maximum continuous sum in right part of the array : [4] SO, CSS for the array [7, -6, 5, 4, -2, 1] = [7, -6, 5, 4] Note : if you start the counting of the sum from the right most part of the array (r) then value of B will be less. Demonstration of the above argument: Sum=0 Sum= sum + 1, , sum=1, B = 1 Sum = Sum + (-2) , sum=-1, B =1 Sum = Sum + 4 , sum=3 , B = 3 So, this proves that CSS should be calculated from the mid element.
@priyadharshinig4527
@priyadharshinig4527 3 жыл бұрын
@@devanshmesson2777 that was a detailed explanation. Understood the concept. Thanks a lot :)
@devanshmesson2777
@devanshmesson2777 3 жыл бұрын
@@priyadharshinig4527 I am glad that you understood!.
@anirudhatkuru9643
@anirudhatkuru9643 Жыл бұрын
are u correct my bro
@joshwzr1257
@joshwzr1257 Жыл бұрын
Thenx
@momtajakhter7460
@momtajakhter7460 3 ай бұрын
please stop confusing us man.
@HimanshuKumar-xz5tk
@HimanshuKumar-xz5tk Жыл бұрын
first approach will be n^3
@user-sf4hi1by4e
@user-sf4hi1by4e 6 ай бұрын
how css is being -1 in 5.18 minutes. can u help
@ashishcode
@ashishcode 6 ай бұрын
I think I had made a mistake during that part, I have even mentioned that in the comments. Just focus on the explanation of how it's working after that it's simple arithmetics.
@md.jahangiralam4273
@md.jahangiralam4273 2 жыл бұрын
You can easily calculate the CSS part for -2 -5 6 -2 here we divide from 6 as the array was[0=>-2 1=>-5 {2=>6} 3=>-2] So, from 6 to left we will sum, and after 6 to right we will sum too. From 6 to left we have -2 - 5 6 Here, 6+(-5) = 1 As the value is decreasing from 6 so we will avoid the sum as we didn't sum so, from 6 to left we have only the value 6 Now, after 6 to right, we will sum too. as we don't have any more elements except -2 so, after 6 to right, we have only the value -2. so, sum for CSS is =6 to left value +after 6 to right value = 6 + (-2) = 4 it is(4) our CSS for -2 - 5 6 -2 this array LSS = -2 RSS = 6 CSS = 4 As the 6 is the max value so we will return 6 for -2 - 5 6 -2 -3 1 5 -6 this array and it will be LSS for this array.... *********************** Same way for -3 1 5 -6 here we divide from 1 as the array was[0=>-3 {1=>1} 2=>5 3=>-6] from 1 to left we have -3 1 sum, -3+1=-2 value decreasing so avoid sum from 1 to left we got (1) after 1 to right we have 5 -6 sum, 5+(-6) =1 value decreasing so avoid sum after 1 to right, we got(5) So, sum the left(1) and right(5) value CSS = 1+5 = 6 we had, RSS = 1 LSS = 5 Max 6 As the 6 is the greater value so we will return 6 for -2 -5 6 -2 -3 1 5 -6 this array and it will be RSS for this array.... **************************** -2 -5 6 -2 -3 1 5 -6 this array LSS = 6 RSS = 6 For CSS from -2 to left -2+6 = 4 increasing value so continue the sum 4+(-5)=1 value decreasing so avoid sum...x left side (4) Right side after -2 to right -3 1 5 -6 -3+1=-2 increasing value so continue the sum -2+5=3 increasing value so continue the sum 3+(-6)=-3 value decreasing so avoid sum...x Right side(3).... sum for CSS = 4+3=7 So,CSS has max value & will be Maximum Sum SubArray = 7 array will be 6 -2 -3 1 5 l|||||ll|||||ll|||||ll|||||l
@sumainakyasar8857
@sumainakyasar8857 3 жыл бұрын
How does the css become - 1(at 5.11)?
@ashishcode
@ashishcode 3 жыл бұрын
Check the description,I made a mistake there
@sumainakyasar8857
@sumainakyasar8857 3 жыл бұрын
Can u show us its algorithm?
@mrtech2075
@mrtech2075 Жыл бұрын
5:04 what are you saying bro . please make viewers understand . not yourself
@Awareness_is_Light
@Awareness_is_Light 7 ай бұрын
your video's volume is not good, its low.
@shibinfr
@shibinfr 3 жыл бұрын
Confusing!!
@atharvabhoirekar6575
@atharvabhoirekar6575 2 жыл бұрын
I think he made a mistake 5:01, shouldnt css = 1 here
@ashishcode
@ashishcode 2 жыл бұрын
Read the description man, I have mentioned it there.
@movocode
@movocode Жыл бұрын
You are using HP laptop 🤔
@ashishcode
@ashishcode Жыл бұрын
Yes so?
@lazry1773
@lazry1773 Жыл бұрын
goat
@faizanhaider6522
@faizanhaider6522 2 жыл бұрын
Arigato
@ashishcode
@ashishcode 2 жыл бұрын
Nande monai desu
@AnshKaurms
@AnshKaurms 3 ай бұрын
it was too confusing
@59_adityapurkar84
@59_adityapurkar84 5 ай бұрын
You confused me
@Harish_00007
@Harish_00007 19 күн бұрын
un alupa pathavan
@Dark2115azazel
@Dark2115azazel Жыл бұрын
jab videos me thoda loud bolne kya effort nahi kar sakthy thy tho video upload krne ka effort kyu kr rhe ho kam se klam thoda loud thobolo ab haar baar zaroori nahi ki viewer ke pass headphone ho
@ashishcode
@ashishcode Жыл бұрын
Bro, this video is 2 years old, i didn't have a mic or anything at that point i only had earphones, now i have a proper mic.
@Dark2115azazel
@Dark2115azazel Жыл бұрын
@@ashishcode ohk then you can re upload these videos again
Algorithms Lecture 13: Maximum Sub-array Problem using Divide-and-Conquer
18:40
Ghassan Shobaki Computer Science Lectures
Рет қаралды 77 М.
КАРМАНЧИК 2 СЕЗОН 5 СЕРИЯ
27:21
Inter Production
Рет қаралды 584 М.
ДЕНЬ РОЖДЕНИЯ БАБУШКИ #shorts
00:19
Паша Осадчий
Рет қаралды 4,9 МЛН
Sigma Girl Education #sigma #viral #comedy
00:16
CRAZY GREAPA
Рет қаралды 77 МЛН
8. NP-Hard and NP-Complete Problems
31:53
Abdul Bari
Рет қаралды 1,8 МЛН
Closest Pair of Points (Divide and Conquer) Explained
8:45
How He Cracked Google in 3rd Year | Tips for DSA 🔥
15:27
Ashish Kumar
Рет қаралды 18 М.
Why Is This Happening?! Floating Point Approximation
5:46
Kadane's Algo in 16 minutes || Algorithms for Placements
16:58
CodeHelp - by Babbar
Рет қаралды 162 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 589 М.
The Backtracking Blueprint: The Legendary 3 Keys To Backtracking Algorithms
13:44
КАРМАНЧИК 2 СЕЗОН 5 СЕРИЯ
27:21
Inter Production
Рет қаралды 584 М.