Maximum Sum SubArray (Kadane's algorithm) (Largest Sum Contigous SubArray)

  Рет қаралды 92,653

Vivekanand Khyade - Algorithm Every Day

Vivekanand Khyade - Algorithm Every Day

Күн бұрын

Пікірлер: 184
@avinashkumar3643
@avinashkumar3643 3 жыл бұрын
Best Explanation of Kadane's Algo I have ever seen. The most important thing that you explained the algo into two parts - 1. Find the greatest sum 2. Find the starting and ending indices of subarray containing the max sum
@tanmayagarwal8513
@tanmayagarwal8513 3 жыл бұрын
I love the way how innocently he teaches.
@DAVISBENNYMIS
@DAVISBENNYMIS 5 жыл бұрын
(2 Line solution ) :- int Kadane(int[] arr){ int localmax=arr[0]; int globalmax=arr[0]; for(int i=1;i
@ajayshukla7238
@ajayshukla7238 5 жыл бұрын
nice one
@premkumareaswaran6875
@premkumareaswaran6875 4 жыл бұрын
What's n here? If it is the length of the array, I get 22 for the above array that Vivekanand has used for the tutorial. Did you try using it before posting the 2-line answer?
@sandip_kanzariya8476
@sandip_kanzariya8476 2 жыл бұрын
Superb solution ☺️😃 enjoy 🤠 Can you explain this solution ?
@armanmanukyan1970
@armanmanukyan1970 3 жыл бұрын
After wandering in a lot of youtube channels, eventually I've found the best explanation here. Thanks.
@gautamtyagi8846
@gautamtyagi8846 3 жыл бұрын
brilliant explanation! the big thing is he makes it much easier to understand. thanks a lot.
@jitendarsahani11
@jitendarsahani11 5 жыл бұрын
Bahut dino se struggle kr rha tha...lekin aaj samaj mei aa gaya. Thanx...
@mahesh_kndpl
@mahesh_kndpl 4 жыл бұрын
The way you explain is so clear. Thanks man.
@dascalucosmin6137
@dascalucosmin6137 6 жыл бұрын
Thank you for explaining the Maximum Sum Subarray algorithm in such an easy way. You are really good teacher! Keep up the good work!!!
@vaishnavia.n.312
@vaishnavia.n.312 3 жыл бұрын
the way u explain is crystal clear, thank nu so much @vivek.
@singarajusureshkumar2330
@singarajusureshkumar2330 2 жыл бұрын
one n only one best explaination of kadane's algo
@SunilKumar18
@SunilKumar18 7 жыл бұрын
Great work man. Brilliant explanation. Please keep doing more videos. hope your channel grows.
@LarryRuane
@LarryRuane 7 жыл бұрын
Great explanation, I love it. One small simplification may be that max_so_far can be initialized to zero, rather than a[0]. Another advantage of making that change is that if the array is zero-length (size==0) you won't be accessing invalid memory.
@michaele5757
@michaele5757 2 жыл бұрын
you can handle an empty array with a base case that checks for an empty array and simply returns (i.e. if (arr.length === 0) return). If you initialize max_so_far to 0, a test case of [-2] (or any negative number) will fail. I used [-2] as an example because that's the test case I failed on Leetcode lol
@princekumarmaurya1763
@princekumarmaurya1763 3 жыл бұрын
Best vedio I have ever seen for this solution 👍👍👍👍👍👍
@christmas_fox-marychristma2801
@christmas_fox-marychristma2801 4 жыл бұрын
Thank you so much for posting this videos! You have such clear explanations!
@AbhishekJain-pm2jn
@AbhishekJain-pm2jn 3 жыл бұрын
Thank you sir for explaining in such a easy way 🙏
@b.saisrinivas1636
@b.saisrinivas1636 3 жыл бұрын
Best explanation that i have seen for this algo
@chetanshrivastava3762
@chetanshrivastava3762 4 жыл бұрын
Nice explanation sir...You have great patience which is must for a programmer. Regarding this example,we can take a lesser size array to save time.
@nikhilbhatt9823
@nikhilbhatt9823 5 жыл бұрын
I think for efficient subarray it should be **if(max_ending_here
@meherkhan933
@meherkhan933 6 жыл бұрын
You are great Sir! You make so simple with your extraordinary explanation! Thank you very much!!!
@Piggybacking
@Piggybacking 2 жыл бұрын
Thank you so much ! your explanation is so helpful.
@Dragondavid
@Dragondavid 6 жыл бұрын
What if all elements are negative value? How do you dandle max_ending_here < 0 ? How would you track indices?
@komuravellyvenky
@komuravellyvenky 5 жыл бұрын
Above algorithm will not work if all the elements are negative. Please refer : www.techiedelight.com/maximum-subarray-problem-kadanes-algorithm/
@tejeswarsahu2498
@tejeswarsahu2498 5 жыл бұрын
This info is very helpful...
@edwardcerverizzo7363
@edwardcerverizzo7363 5 жыл бұрын
Solution is trivial. Return an empty array. Sum of all elements is zero (since there are no elements). 0 is strictly greater than any combination of negative numbers. You could create a subroutine to scan the array and return this result if this is the case. Total running time should be O(2n) which is still O(n).
@jogeswararaonynala1591
@jogeswararaonynala1591 5 жыл бұрын
Then just take the element with with highest value in all negative numbers
@sharidass1408
@sharidass1408 4 жыл бұрын
@@komuravellyvenky this code also fails if array is [-1]
@rhimanshu6288
@rhimanshu6288 3 жыл бұрын
You made things understand easier
@PramodKumar-rc9zy
@PramodKumar-rc9zy 6 жыл бұрын
Nice explain sir before watching this video i was confused that what the meaning of this algo but now cleared thanks a lot sir
@LarryRuane
@LarryRuane 7 жыл бұрын
One more observation ... What if the array consists entirely of negative values? This algorithm will report start and end both zero, which is a subarray of length 1. It seems like it may be better, in general, for the result to be reported as a start index and a length (rather than end index). Then the correct answer in this case (all elements negative) is start = zero (or really anything), length = zero. I implemented this here: codepad.org/irM2hs1b
@Mohitsingh-mk8vr
@Mohitsingh-mk8vr 2 жыл бұрын
best explanation of kadane algo
@sanketkumar1576
@sanketkumar1576 7 жыл бұрын
best explanation among all videos on this topic on youtube. thank you !
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Thanks Sanket..!
@Dyslexic_Neuron
@Dyslexic_Neuron 5 жыл бұрын
your understanding of the problem is Bad..... The way I see the problem is 3 cases : Life Hope and Death. We'll take 2 sum variables' : prev_sum and current_sum. Life is when we encounter only positive number: we will update prev_sum in this case Hope is when we encountered a negative number but that negative number has not made my current sum less than zero so there is still hope that some next number may repair the damage done by the negative number. : we will update prev_sum when current_sum > prev_sum Death is when the current sum becomes negative .....so now in this case we have re-initialize the starting point for calculating current sum Below is my smart : : code public static int findLargestSum(int a[]) { int end = 0; int current_sum = 0; int prev_sum = 0; while (end < a.length) { current_sum += a[end]; if (current_sum < 0) { // death case current_sum = 0; } if (current_sum > prev_sum) { prev_sum = current_sum; } end++; } return prev_sum;
@manojbgm
@manojbgm 7 жыл бұрын
Nice Video. Well structured. Liked the way you simplified the solution in 2 parts. 1) find the max sum 2) look for index Nice work thank you.....
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Thanks Manoj..!
@rahul-patil
@rahul-patil 6 жыл бұрын
The second if condition is the core of this algo.
@prudhviprasad6386
@prudhviprasad6386 3 жыл бұрын
Thnks sir it was very detailed explanation
@anindita2816
@anindita2816 5 жыл бұрын
Very good explanation. You're that favorite teacher kind of person! :)
@koreancaptain2955
@koreancaptain2955 5 жыл бұрын
han rat lo ise
@bharatprakashparakh9601
@bharatprakashparakh9601 4 жыл бұрын
kzbin.info/www/bejne/o6DXfnt_jbKgsM0
@naveensharma5060
@naveensharma5060 7 жыл бұрын
finally i got the video by which i have understood this concept very easily.
@sailkargutkar1980
@sailkargutkar1980 6 жыл бұрын
Best way to explain max sub array prob thanks
@mani8110
@mani8110 Жыл бұрын
thank you best explanation❤❤
@brijbhushanawasthi7679
@brijbhushanawasthi7679 7 жыл бұрын
Set the speed to 1.5, Thank me later:)
@RahulGuptarrg
@RahulGuptarrg 7 жыл бұрын
Thank u bro :)
@shobitshobit
@shobitshobit 6 жыл бұрын
Thank u bro :)
@LV-ei1ce
@LV-ei1ce 6 жыл бұрын
Lol ! Thank you bro :)
@stndwlf5862
@stndwlf5862 5 жыл бұрын
Thank U Bro :)
@Rahulsingh-bu6jh
@Rahulsingh-bu6jh 5 жыл бұрын
i suggest 2.0x
@dorararo
@dorararo 3 жыл бұрын
can we print the largest sub-array that was found , using Kadane algo?..we can get the end point of that sub-array but how do we get its starting point.
@rajporu9
@rajporu9 7 жыл бұрын
Vivek, the way you explain is crystal clear by giving examples. Keep up the good work. God Bless.
@Ankit13555
@Ankit13555 7 жыл бұрын
you are simple and great....plzz provide some good and tuff DP problems ...:)
@tapanjeetroy8266
@tapanjeetroy8266 5 жыл бұрын
Thanks a lot lot sir..you explain us so well
@harshtulsyan9894
@harshtulsyan9894 7 жыл бұрын
Nice Explanation sir... Please upload more videos on Dynamic Programming..!!
@SmartProgramming
@SmartProgramming 6 жыл бұрын
awesome explanation sir, very very helpful, thanks a lot for this tutorial 👍👍👍👍🙂🙂🙂🙂
@ravikishoretottaramudi1263
@ravikishoretottaramudi1263 2 жыл бұрын
Very good explanation sir
@shivam_0002
@shivam_0002 6 жыл бұрын
Great Explanation, make some more videos. Thanks
@somesbhowmick2082
@somesbhowmick2082 5 жыл бұрын
Great , Great simply you explain I understand, keep doing it , I want learn more algorithm topic probmel
@prakashkumaran9767
@prakashkumaran9767 5 жыл бұрын
Good work. Easy to learn. Thank you..
@rushi901
@rushi901 6 жыл бұрын
Any idea about how to convert the finding the start index and end index into 2 D array ?
@edwardcerverizzo7363
@edwardcerverizzo7363 5 жыл бұрын
I've seen this problem posted as a dynamic programming problem on Leetcode. I like this way better though. Is there any argument to do DP over this way?
@shobhitmittal77
@shobhitmittal77 7 жыл бұрын
Hi Vivekanan sirji, you are doing a great job.. I have watched some of your other videos too and must say they are simply awesome and to the point.. It will be even better if you could organize your uploaded videos into a playlist, it will direct the users to your other videos.... just a suggestion :)
@srinidhiii
@srinidhiii 7 жыл бұрын
well said.it ll be great if a playlist is created topicwise.Your tutorial is awesome sir
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Yes shobhit i will make it...! Thanks ....!
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Yes srinidhi ...will make it..!
@abhishekbisherwal6856
@abhishekbisherwal6856 5 жыл бұрын
NO need to check whether the sum is less than zero or not else do check whether calculated sum is greater than added element or not ? and rest will be same .
@karthikd7460
@karthikd7460 4 жыл бұрын
After seeing this video, I feel your one of the LC problems god!!!
@swapnil814
@swapnil814 5 жыл бұрын
Thank you. You made tough problem, easy. :)
@dharnisingh11
@dharnisingh11 4 жыл бұрын
What if we do not have any negative number present in the array?
@ASHISHSINGH-nj6es
@ASHISHSINGH-nj6es 5 жыл бұрын
Algorithm tracing != Explaining
@TanujMishra077
@TanujMishra077 7 жыл бұрын
Great work Sir. Nice explanation.
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Thanks Tanuj.
@beholdpain
@beholdpain 5 жыл бұрын
Nice, Very Detailed!
@tirnasircar2938
@tirnasircar2938 4 жыл бұрын
What will happen if there are no negative numbers in the array?
@sagarsalunkhe6429
@sagarsalunkhe6429 7 жыл бұрын
Thanks for explanation very well done
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Thanks Sagar Bhau...!
@MyLifeMyWorld08
@MyLifeMyWorld08 6 жыл бұрын
Hello Sir, Can you please post video for solution of "maximum/minimum element from each sub-array of size 'k'" in O(n) ? Thanks in advance.
@aryangarg9072
@aryangarg9072 4 жыл бұрын
Good explanation
@ghughal
@ghughal 5 жыл бұрын
Will this algorithm work for an input array {5, -2, -1, 3, -4}. Just a second set of eyes to verify we get the result as maximum length of the subarray is 4. Please help!
@niharikaepuri3305
@niharikaepuri3305 7 жыл бұрын
Can you please make a video on Largest subarray with equal number of 0s and 1s with O(N) time complexity and also please make a video on Maximum Product Subarray?
@jakusam4564
@jakusam4564 6 жыл бұрын
sir thank you very much.i am from bangladesh.
@yunuskocatas3879
@yunuskocatas3879 2 жыл бұрын
why we equalise the max ending here to zero on second if
@rajatmishra3572
@rajatmishra3572 4 жыл бұрын
nice explanation!!
@hirakmondal6174
@hirakmondal6174 4 жыл бұрын
faulty explanation.. will fail for sample case like [-2 5 -1] where the output of the maxsum with his logic is 3 but it should be 5 actually. *max_so_far* at the starting should be the maximum element of the array.
@manishgurawalia7625
@manishgurawalia7625 4 жыл бұрын
No it will not fail here, will give 5 as the the max sum
@YogeshKumar-ye8nd
@YogeshKumar-ye8nd 7 жыл бұрын
if I will take only all elements negative except first then this code will not give the index of maximum subarray you can check it
@shankysays
@shankysays 3 жыл бұрын
If my array is -5 -4 -3-1 -2 will this algo work?
@anshubansal2008
@anshubansal2008 6 жыл бұрын
Great Explanation.
@shivamvyas8899
@shivamvyas8899 4 жыл бұрын
Hello sir, can u make a video on Z algorithm for String search.
@baibhavghimire3827
@baibhavghimire3827 6 жыл бұрын
Nice one..Yes need to do like 1.5x or 2x..lol...But great presentation!!
@arbindsharma1423
@arbindsharma1423 3 жыл бұрын
sir how to approach the algo please teach that not as ratta mar study
@DurgaShiva7574
@DurgaShiva7574 3 жыл бұрын
will this algo works if we have the max sum in -ve itself....i.e if all the elements of the array are -ve, then what to do ?
@yashwanthjonnagaddala9944
@yashwanthjonnagaddala9944 3 жыл бұрын
No this works for everything
@mohit0001ish
@mohit0001ish 6 жыл бұрын
Very Well Explained :)
@mordiabukarat3985
@mordiabukarat3985 6 жыл бұрын
very helpful , thank you
@Hampirathna
@Hampirathna 4 жыл бұрын
Sir plz explain dijkstras algorithm with snippet like this sir
@DhananjayTyagi24
@DhananjayTyagi24 5 жыл бұрын
Explained well!
@alokkumarsingh4118
@alokkumarsingh4118 4 жыл бұрын
Sir will the algorithm work if the maximum sum is negative?
@saurabhsharma8577
@saurabhsharma8577 6 жыл бұрын
Nice Work, Thank you Sir
@bihanbanerjee
@bihanbanerjee 3 жыл бұрын
thank you
@shimpyasthana5561
@shimpyasthana5561 5 жыл бұрын
Great work
@saswatibhattacharjee7387
@saswatibhattacharjee7387 5 жыл бұрын
Is Kadane's Algorithm applicable if all the element in the array are positive integer? I have an array like {4,2,1,10,3,5} when I am using this algorithm it return 25 as sum and all the elements . I cannot find any sub array. If I add 10+3+5=18 ,get 18 which is the largest sum in this array also {10,3,5} represent a sub array. For this problem what type of algorithm applicable? Your explanation is very clear and easily understandable. Thank you.
@sauravbhagat4737
@sauravbhagat4737 5 жыл бұрын
If array contains all positive numbers, then sum of all elements will be maximum. So the sub array will be the whole array.
@sivas8620
@sivas8620 7 жыл бұрын
Please do a video Tutorial on Flatten a Linkedlist, Union of 2 Linkedlist
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Yes sure I will do it..!
@Muthuvlog104
@Muthuvlog104 2 жыл бұрын
Good explanatiin
@ayonkar1534
@ayonkar1534 7 жыл бұрын
What if all the elements in the array is set to 0?
@gaymonkey5270
@gaymonkey5270 6 жыл бұрын
What if all values are negative?????
@compeng2013
@compeng2013 6 жыл бұрын
it will return the greatest negative number. So if you have an array = [-6, -5, -20, -1], it will return -1
@sauravbhagat4737
@sauravbhagat4737 5 жыл бұрын
@@compeng2013 No it is going to return 0, we need to modify the code little bit
@jogeswararaonynala1591
@jogeswararaonynala1591 5 жыл бұрын
Just take the highest elment in the negative no
@argharoy6571
@argharoy6571 4 жыл бұрын
Sir you are awesome
@muhammahanisuzzaman5493
@muhammahanisuzzaman5493 4 жыл бұрын
Carry On...
@mohamedanwar6073
@mohamedanwar6073 7 жыл бұрын
please do video , print all different simple cycle in undirected graph . 🙌
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
yes sure ...very soon..!
@MrVazanth
@MrVazanth 7 жыл бұрын
Hi Vivekanand, Please help me with video to print given matrix in diagonal order Thanks
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Yes i will upload the matrix video very soon..!
@modernsanskari4398
@modernsanskari4398 7 жыл бұрын
grt sir .But this question was asked in my interview but i was not aware of this algo.But i gave a brute force solution by using two loops O(n^2).Here in this algo it is taking linear time o(n). Can we make it to O(log n) by using divide and conquer approach?
@santhoshcts5
@santhoshcts5 7 жыл бұрын
if the array is not sorted , we cannot do it in o(logn) . in other way , meaning only binary search will make time complexity of o(logn) and binary search will work only on sorted arrays .
@Alexc99xd
@Alexc99xd 6 жыл бұрын
You could divide and conquer in O(nlog(n)) recursively Find max on left, max on right, and find max that crosses the midpoint
@Sudhanshusable98
@Sudhanshusable98 4 жыл бұрын
Thank you sir 🙏
@prateekkanujiya9775
@prateekkanujiya9775 5 жыл бұрын
Awesome 👍
@yunuskocatas3879
@yunuskocatas3879 2 жыл бұрын
perfect explenentation
@superchillh3o
@superchillh3o 7 жыл бұрын
excellent explanation, thank you sir
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Thanks Alberto..!
@smirkedShoe
@smirkedShoe 7 жыл бұрын
Where was the explanation of logic in this video... He only puts value and iterate through the loops...Everyone can do that..Please explain the logic!
@shrijanaryal4923
@shrijanaryal4923 7 жыл бұрын
The logic is that you have two variables as you go through the loop. One calculates temp_max and the other max_so_far. As soon as temp_max becomes greater than max_so_far, make max_so_far equals to temp_max. But remember temp_max may not always be greater. And also when temp_max is negative, set it to 0 because you dont want to disturb the previous max by some negative numbers. I hope it helps..
@kapilchhipa2143
@kapilchhipa2143 6 жыл бұрын
gandu dekh kahe rha h phr
@niranjankadu4132
@niranjankadu4132 6 жыл бұрын
giving wrong o/p=9 for {1,2,3,-2,5} correct o/p is {1,2,3}=6
@akash7752
@akash7752 4 жыл бұрын
will not work for all negative no in an array
@karanshah9310
@karanshah9310 4 жыл бұрын
Longest Palindrome in a string
@sireeshakandula1031
@sireeshakandula1031 7 жыл бұрын
Biconnecting components and dfs
@josephmorales652
@josephmorales652 7 жыл бұрын
Thanks man!
Find minimum (Smallest) element in Array
5:38
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 88 М.
Segregate 0's, 1's and  2's together in an array[O(n)](Dutch National Flag Problem)
17:27
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 61 М.
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 112 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 18 МЛН
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 495 М.
Longest Common Subsequence Dynamic Programming : Interview question
19:52
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 55 М.
Maximum Product Subarray - Dynamic Programming - Leetcode 152
15:31
Cutting a rod into pieces to maximize Profit (Dynamic Programming)
28:49
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 63 М.
Minimum jumps to reach end of array (Dynamic Programming)
26:46
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 85 М.
Kadane's Algorithm to Maximum Sum Subarray Problem
11:17
CS Dojo
Рет қаралды 723 М.
Kadane's Algo in 16 minutes || Algorithms for Placements
16:58
CodeHelp - by Babbar
Рет қаралды 204 М.
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 112 МЛН