Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing

  Рет қаралды 491,246

take U forward

take U forward

Күн бұрын

Пікірлер: 492
@takeUforward
@takeUforward Жыл бұрын
Please watch our new video on the same topic: kzbin.info/www/bejne/d3m9oax7g9RqoZY
@whateverittakes5340
@whateverittakes5340 3 ай бұрын
It's recursion!!! lol i kept on clicking the link and its the same link (not recursion but yeah a loop)
@lavanya_m01
@lavanya_m01 9 ай бұрын
13:56 "Do not carry any negatives into your future" - Striver Even thought the context was different, it can be applied in our real life❤
@KrishnaAgrawal-hw5lk
@KrishnaAgrawal-hw5lk 5 ай бұрын
TRUE
@DhananjayKumar-bd2jg
@DhananjayKumar-bd2jg 3 ай бұрын
Nice Humor
@takeUforward
@takeUforward Жыл бұрын
Let's march ahead, and create an unmatchable DSA course! ❤ Use the problem links in the description.
@princejatav8456
@princejatav8456 Жыл бұрын
bro in case of printing we must have keep track of only last index, think of it, bcz if after getting maxsum and startind and endind of subarray , if we further get the sum to be negative and then we put sum=0 and startindex to that index and think if we dont get any subarray that have sum greater than previous one, then we lost our startind and endind of main subarray ,then this will give wrong answer so correct solution for it will be keep only track of endind, so next time when we get higher sum then only change it. and after getting maxsum we can easily get our subarray by using last ind, by going on left side of ind and rightside of ind
@Nainalarenukadevi9196-dh8rz
@Nainalarenukadevi9196-dh8rz 4 ай бұрын
hey is it same for longest subarray sum
@chethanprabhu4475
@chethanprabhu4475 Жыл бұрын
Couple of years back, I had watched the best video on KZbin(in terms of views) on Kadanes and still it was not very clear to me. And this video is so so better than the other video. Top level walkthrough. P.S: I am not comparing. Else I would have told which video was that which I watched earlier :)
@rishabh1S
@rishabh1S Жыл бұрын
Always ready for Dsa ✅
@ashishshetty3512
@ashishshetty3512 Жыл бұрын
Haan Bhai
@THEReFleX
@THEReFleX Ай бұрын
jhuk fir
@rpanda_old
@rpanda_old Жыл бұрын
weird how this explanation of kadans algo is so simple compared to other yt videos. short algo short code. superb
@shubhamagarwal1434
@shubhamagarwal1434 3 ай бұрын
#Free Education For All.. # Bhishma Pitamah of DSA...You could have earned in lacs by putting it as paid couses on udamey or any other elaerning portals, but you decided to make it free...it requires a greate sacrifice and a feeling of giving back to community, there might be very few peope in world who does this...."विद्या का दान ही सर्वोत्तम दान होता है" Hats Off to you man, Salute from 10+ yrs exp guy from BLR, India....
@harigs72
@harigs72 2 ай бұрын
I did it by myself ..😅i found the logic after 10min I started..it feels so good . I don't know this problem is hard or not
@manipandit18
@manipandit18 Жыл бұрын
Time Stamp 0:00 - Introduction to course 0:41 - Problem Statement 2:13 - Brute Force Solution 6:12 - Better Solution 7:40 - Optimal (Kadane's Algorithm) 13:18 - Code 15:29 - Time Complexity 15:40 - Follow up question 19:37 - Outro There's always something new to learn from striver's videos . Thank You bhai for posting videos without any long gap!!!.
@arnavjain762
@arnavjain762 9 ай бұрын
Complete concept clarity in 20 mins. Amazing ✅✅✅✅
@vaishnaviganseh2884
@vaishnaviganseh2884 Жыл бұрын
BEST Kadane's algo video on the internet!
@Manishgupta200
@Manishgupta200 Жыл бұрын
Kadame's Algorithm is now clear. Thankyou Striver ❤ From brute(TC -> O(N^3), SC -> O(1)) to better(TC -> O(N^2), SC -> O(1)) to Optimal(TC -> O(N), SC -> O(1))
@3rd_Eye_Gang
@3rd_Eye_Gang Жыл бұрын
Nothing can describe how thankful we're to you for such amazing content.. . God Bless you Striver.. Hope you achieve everything you want ❤️❤️
@bendivanitha7211
@bendivanitha7211 Жыл бұрын
"Understood " superb intuition of algorithm !! awsome explanation i request everyone whoever watching strivers vedeos do like and comment!!!
@Raj10185
@Raj10185 Жыл бұрын
Printing the subarrays part is something i learn this time tysm understood:)
@tarunsingh4413
@tarunsingh4413 10 ай бұрын
at 15:22 you have to add this code in for loop . if(maxi
@pradeepsahu5500
@pradeepsahu5500 2 ай бұрын
yup it will not work if every element is negative so in the end max will be negative but we want 0 as answer so i did it myself (added that condition )after the for loop
@chaithrac370
@chaithrac370 5 ай бұрын
Really amazed by the effort you put into making us understand. Thank you, Striver!
@yugamsaini8761
@yugamsaini8761 Жыл бұрын
Bro its 2pm night in India, You are doing great Job, consistency 💥
@rohitprasad5708
@rohitprasad5708 Жыл бұрын
He is in warsaw, which means he uploaded the video between 9:30-10 pm in his time
@takeUforward
@takeUforward Жыл бұрын
Yes I could not do it during the morning today. Came back from office and recorded, edited and uploaded 😄
@uncannyroaches5933
@uncannyroaches5933 Жыл бұрын
Am hai🤭🤭🤭
@rohitprasad5708
@rohitprasad5708 Жыл бұрын
@@takeUforward Thats why your content is the best
@yelururao1
@yelururao1 Жыл бұрын
i saw many videos but not able to understad.... this video gave me complete understading..Thanks bro
@Ritik7_77
@Ritik7_77 2 ай бұрын
Great teaching by great teacher ❤
@RaviKumar-sn6tu
@RaviKumar-sn6tu 8 ай бұрын
in love with kadane algorithm...all thanks to you bhaiya
@dhanviakash726
@dhanviakash726 11 күн бұрын
Seriously, I really understood this 😮 The intuition is just common sense lol, If we add negative subarray sum to the next negative element it will decrease the value of our new subarray sum, even if we add negative subarray sum to our next positive element it will again decrease the value of our new subarray sum
@stith_pragya
@stith_pragya 5 ай бұрын
Understood....Thank You So Much for this wonderful video.....🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@SydMohan
@SydMohan Жыл бұрын
Love your explanation of progression of solutions and code walk through. Please keep making precise and amazing content like this. It really helps to stay motivated with solving problems because when I'm stuck, the logic in your vids is explained very clearly. Thanks a lot!!
@sohilpathan9006
@sohilpathan9006 Жыл бұрын
If you're doing on leetcode where negeative also consider where code follows, int maxSubArray(vector& nums) { int cs =0,ms=nums[0]; for(int i=0; i< nums.size(); i++){ cs += nums[i]; ms = max(ms,cs); if(cs < 0) cs = 0; } return ms; }
@KarthikAsam-q3r
@KarthikAsam-q3r Жыл бұрын
bro i really love your explanation ;how ever i explane doudtes to my frnds you explaining in same manner ❤
@factfortune2160
@factfortune2160 10 ай бұрын
Easiest explaination ever👌👌 thanks bhaiya...!!
@cinime
@cinime Жыл бұрын
Understood! Super excellent explanation as always, thank you very very much for your effort!!
@tasneemayham974
@tasneemayham974 Жыл бұрын
BEST TEACHERRRR EVERR!!!!!!!!!!
@SurajGupta-gc9tz
@SurajGupta-gc9tz Жыл бұрын
i was very keen about learning DSA and your sheet and your explanation has boosted this thank you strive bhaiya
@manjamalaimm6178
@manjamalaimm6178 9 ай бұрын
Thank You so much for made this crystal clear understanding about this problem.
@sahadevbhaganagare4761
@sahadevbhaganagare4761 11 ай бұрын
The result will never be lesser than zero because one condition is "if(sum>max) max=sum" and initially sum=0 and max=LONG_MIN. hence, max will always be zero if all the values in the array are negative.
@user-zr3eu2oo7s
@user-zr3eu2oo7s 7 ай бұрын
It is returning maxi variable so even if it is sum is negative and we make sum zero and then add a negative number to sum it will be lesser than the maxi variable
@abhir4872
@abhir4872 5 ай бұрын
no it will return the biggest -ve number if there are all -ve in array
@Mythri333
@Mythri333 Ай бұрын
if all elements are negative it returning the biggest negative value because even we keep sum=0 when sum if(maxi
@suyashshinde2971
@suyashshinde2971 Жыл бұрын
SDE Sheet Day 1 Problem 1 Done!
@syedFAHIM-el1wr
@syedFAHIM-el1wr Жыл бұрын
Crystal Clear Understanding !
@radhakishoriiiiiiiiiii7348
@radhakishoriiiiiiiiiii7348 Жыл бұрын
12:28 very nice explaination. Very helpful walk through that cleared my confusions
@AleksandrStrizhevskiy
@AleksandrStrizhevskiy Ай бұрын
Thank you for this video, great explanation! I will need it for my exam in an hour haha!
@allegragarland9156
@allegragarland9156 11 ай бұрын
Im new to programming and this was very helpful ~
@embarrassed_dodo
@embarrassed_dodo 4 ай бұрын
Very detailed explanation, it is language agnostic as well, thanks for the video
@loneleyEngineer
@loneleyEngineer Күн бұрын
Living in this era where striver exists is like living in era where Ronaldo and messi are alive together. I can't thank enough!
@shivamsingh-we7ek
@shivamsingh-we7ek Жыл бұрын
i am following this course for my dsa preparation , its an amazing course and explanation by bhaiya just want one thing complete this by end of october ♥♥
@pavankumar-cz9hb
@pavankumar-cz9hb Жыл бұрын
have u completed
@archanaverma8771
@archanaverma8771 Жыл бұрын
Your way of explanation is really outstanding🔥🔥🙌thanks lot and more!!!!
@VarunKaushal-zx9zq
@VarunKaushal-zx9zq Жыл бұрын
Understood, Amazing Lecture Sir!
@sparshverma4030
@sparshverma4030 Жыл бұрын
understood you are the best teacher. 🙌
@mayurkumbhar2729
@mayurkumbhar2729 3 күн бұрын
Great explanation 🎉
@amitmauryarbl2602
@amitmauryarbl2602 4 күн бұрын
Thank you bade bhaiya
@PriyanshuKumar-zd1lq
@PriyanshuKumar-zd1lq Жыл бұрын
Super explanation with so much love 😃
@mrdevil8419
@mrdevil8419 5 ай бұрын
🎯 Key points for quick navigation: 00:43 *🧩 Introduction to the problem of finding the maximum subarray sum* - Definition of a subarray as a contiguous part of an array - Explanation of how to identify subarrays with maximum sum in an array 02:18 *🔄 Brute force approach to finding the maximum subarray sum* - Iterating through all possible subarrays to find the maximum sum - Detailed explanation of the nested loops to generate subarrays and calculate sum - Analysis of time and space complexity for the brute force approach 06:14 *👍 Improved approach to finding the maximum subarray sum* - Utilizing observations to optimize the brute force approach - Updating the sum incrementally without reiterating through each element - Analysis of the improved approach's time and space complexity 07:51 *🚀 Introduction to Kadane's Algorithm for the maximum subarray sum* - Description of the Kadane's Algorithm for finding the maximum subarray sum - Implementation steps of Kadane's Algorithm for optimizing the solution further - Understanding the logic behind dropping negative sums in Kadane's Algorithm 17:06 *📜 Modifying the Kadane's Algorithm to track and print the subarray with maximum sum* - Introduction of additional variables to track the start and end of the subarray - Explanation of how to modify the Kadane's Algorithm code to print the subarray with maximum sum - Discussion on maintaining the time and space complexity while adding printing functionality Made with HARPA AI
@ArunKumar-zp8cp
@ArunKumar-zp8cp Жыл бұрын
Bro really you have good knowledge of DSA...
@manojkumarparuchuri5920
@manojkumarparuchuri5920 19 күн бұрын
excellent explanation thanku so much
@iitmotivationwithrahullson5930
@iitmotivationwithrahullson5930 Жыл бұрын
you are doing great job striver ❤.. .
@UECAshutoshKumar
@UECAshutoshKumar 8 ай бұрын
Thank you 🙏
@saumyapandey8940
@saumyapandey8940 5 ай бұрын
your course is too good
@hareshnayak7302
@hareshnayak7302 8 ай бұрын
Understood,Thank striver for this amazing video.
@helloworld004
@helloworld004 11 ай бұрын
class Solution { public: int maxSubArray(vector& nums) { int maxi=nums[0]; int sum=nums[0]; for(int i=1;i
@hengenaavu5737
@hengenaavu5737 2 ай бұрын
Superb bro, exceptional 🎉🎉🎉
@JamieFord0109
@JamieFord0109 25 күн бұрын
Understood. Thanks Striverr!!1
@hunter-js8hy
@hunter-js8hy 6 ай бұрын
done and dusted ! hats off to striver ..
@kikicode-g5v
@kikicode-g5v Жыл бұрын
15:05 editing mistake 🫡 but no worries
@takeUforward
@takeUforward Жыл бұрын
shit yes, thankfully not a big one
@vish-sw9dc
@vish-sw9dc Жыл бұрын
​@@takeUforward please make a video on long pressed name
@Poojithaalam
@Poojithaalam 8 ай бұрын
@@takeUforward which tool you are using for white boarding?
@prabalsharma3007
@prabalsharma3007 Жыл бұрын
great concepts , understood everything
@swagnikdhar6010
@swagnikdhar6010 Жыл бұрын
Absolutely Amazing ✌️🔥
@samuelfrank1369
@samuelfrank1369 Жыл бұрын
Understood. Thanks a lot. Please upload more videos Bhaiyaaa.
@StudyEmail-p9u
@StudyEmail-p9u 10 ай бұрын
Understood!.Thank you.
@isheep9025
@isheep9025 Жыл бұрын
2 pointer approach to solve the problem ,may give tle where 0(n) is expected: class Solution { public: int sums(int prev,int curr,vector nums) { int s=0; for(int i=prev;i
@piaknow3881
@piaknow3881 Жыл бұрын
Thank you very much bhaiya for these. In upcoming videos please add general approach for techniques like sliding window, two pointers etc techniques as the way you give for recursion and dp etc. Thank you once again bhaiya
@sahanagoudar1647
@sahanagoudar1647 Жыл бұрын
13:57 Don't carry any negatives into future 😇
@tulsilukhi1182
@tulsilukhi1182 6 ай бұрын
Best explanation everr
@Bigg_boss_trolls
@Bigg_boss_trolls Ай бұрын
Nice man good teaching EXELLENT
@vinethasuresh3488
@vinethasuresh3488 Жыл бұрын
1. is the carryforward and sliding window technique is both are same? 2. can you please tell me the difference between carryforward and sliding window technique? it will be really helpful if you explain me sir.
@parica117
@parica117 2 ай бұрын
Thank you striver your videos are helping alot
@manansarraf73
@manansarraf73 3 ай бұрын
Nice explanation bhaiya
@aslamcodes
@aslamcodes Жыл бұрын
8:20 "Do not worry" ✨
@aryanmaniyar3475
@aryanmaniyar3475 Жыл бұрын
Tired of commenting AMAZING bhaiya ;) !! You're tooooo good :)
@ganeshjaggineni4097
@ganeshjaggineni4097 8 күн бұрын
NICE SUPER EXCELLENT MOTIVATED
@AmanPandey-bd1sj
@AmanPandey-bd1sj Жыл бұрын
Thanks brother Best Explanation😊
@shajahan1064
@shajahan1064 Жыл бұрын
in the brute approach is 3 for loops needed? #include using namespace std; // #include int maxSumSubarray_brute(vectorarray){ int prevsum =0; int maxsum = INT_MIN; for(int i=0;i
@EC20022ELAKKIYAC
@EC20022ELAKKIYAC 10 ай бұрын
Completely Understood!
@Ashishvatsav
@Ashishvatsav 9 ай бұрын
Amazing!! Keep going bro⚡
@d3vi980
@d3vi980 6 ай бұрын
nums = [-1, -2, -3, -4] According to your solution, the start will be 3 in this case but the correct answer should be 0
@ayushmandliya9493
@ayushmandliya9493 Жыл бұрын
#include long long maxSubarraySum(int arr[], int n) { //Kadan's Alogrithm ->move and keep adding and if less then 0 and then neglect long long sum=0; long long maxi=LONG_MIN; for(int i=0;imaxi){ maxi=sum; } if(sum
@infernogamer52
@infernogamer52 Жыл бұрын
Understood bhaiya!
@sach_in_sigma
@sach_in_sigma 3 ай бұрын
sir in the follow up question if we take the array as {-1,-2,-3,-4} your solution will give answer as (3,3) whereas the correct answer should have been (0,0)
@YerramArun
@YerramArun Жыл бұрын
Understood ❤bhaiya❤❤
@ashishdhal4614
@ashishdhal4614 Жыл бұрын
Can't wait for binary search series
@U2011-n7w
@U2011-n7w 4 ай бұрын
best video
@tamilmukbang3789
@tamilmukbang3789 7 ай бұрын
Understood.. thank you so much bro
@amarsharma8582
@amarsharma8582 Жыл бұрын
kadane's algorithm c++ code: there was error in editing so i wrote the code this might help beginners like me :) #include using namespace std; int main() { int n = 8; int arr[n] = {-2, -3, 4, -1, -2, 1, 5, -3}; long long sum = 0, maxi = LONG_MIN; int start = 0, end = 0; for (int i = 0; i < 8; i++) { if (sum == 0) { start = i; } sum += arr[i]; if (sum > maxi) { maxi = sum; end = i; } if (sum < 0) { sum = 0; } } for (int i = start; i
@vaishalirawat2447
@vaishalirawat2447 Жыл бұрын
Thanku u
@amarsharma8582
@amarsharma8582 Жыл бұрын
@@vaishalirawat2447 welcome.
@nishant1456
@nishant1456 Жыл бұрын
Striver is Love yr❤
@ITSuyashTiwari
@ITSuyashTiwari Жыл бұрын
simply , loved it....
@AyushSharma-ye1xz
@AyushSharma-ye1xz Жыл бұрын
All videos are very helpful
@culeforever5408
@culeforever5408 Жыл бұрын
understood 😇
@darkshadowcodm6335
@darkshadowcodm6335 5 ай бұрын
Thanks
@konankikeerthi
@konankikeerthi 5 ай бұрын
Understood bro. Thank you
@m.afnan2018
@m.afnan2018 Жыл бұрын
Can it optimise further more ? Follow up question: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
@ritabhsharma6627
@ritabhsharma6627 11 ай бұрын
Man copy pasted the Follow Up of Leetcode on this question.
@prateekmahajan5490
@prateekmahajan5490 7 ай бұрын
Understood. Thanks a ton 😇
@_hulk748
@_hulk748 Жыл бұрын
UNDERSTOOD SIR🙇‍♂❤🙏
@SriSarthak257
@SriSarthak257 6 ай бұрын
Understood striver! 🔥👍
@banothutharun2743
@banothutharun2743 9 ай бұрын
mind blowing sir
@shotbotop3790
@shotbotop3790 4 ай бұрын
For those solving this in 2024 , if you try to submit this on leetcode this will fail on the test case [-1] since obviously our array has only one neg element ,it shall return -1 but in our case it will return 0..., You can refer the following code to solve it instead : int maxi = nums[0]; // Initialize maxi to the first element int sum = nums[0]; // Initialize sum to the first element int n = nums.size(); for (int i = 1; i < n; i++) { sum = max(nums[i], sum + nums[i]); maxi = max(maxi, sum); } return maxi; }
@graviton001
@graviton001 4 ай бұрын
I know it should not work but idk why its working on lc?😅
@graviton001
@graviton001 4 ай бұрын
I added the test case -1 from my side then also its working but it should return 0 LOL.
@vaishalidhokchawle4384
@vaishalidhokchawle4384 7 ай бұрын
Great job👍🏻
@forstudycode6146
@forstudycode6146 Жыл бұрын
Understand Brother.
DP 35. Best Time to Buy and Sell Stock | DP on Stocks 🔥
9:11
take U forward
Рет қаралды 412 М.
Maximum Product Subarray - Best Intuitive Approach Discussed
20:27
take U forward
Рет қаралды 236 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 27 МЛН
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 117 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 97 МЛН
Count Subarray sum Equals K | Brute - Better -Optimal
24:09
take U forward
Рет қаралды 337 М.
Rearrange Array Elements by Sign | 2 Varieties of same Problem
21:37
take U forward
Рет қаралды 226 М.
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 15 М.
3 Types of Algorithms Every Programmer Needs to Know
13:12
ForrestKnight
Рет қаралды 501 М.
10 Math Concepts for Programmers
9:32
Fireship
Рет қаралды 1,9 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 27 МЛН