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

Поділитися
Вставка
  • Опубліковано 10 січ 2025

КОМЕНТАРІ • 529

  • @takeUforward
    @takeUforward  Рік тому +31

    Please watch our new video on the same topic: ua-cam.com/video/AHZpyENo7k4/v-deo.html

    • @whateverittakes5340
      @whateverittakes5340 5 місяців тому +8

      It's recursion!!!
      lol i kept on clicking the link and its the same link (not recursion but yeah a loop)

    • @hearsid
      @hearsid 20 днів тому

      Delete the link

  • @lavanya_m01
    @lavanya_m01 10 місяців тому +304

    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❤

  • @takeUforward
    @takeUforward  Рік тому +142

    Let's march ahead, and create an unmatchable DSA course! ❤
    Use the problem links in the description.

    • @princejatav8456
      @princejatav8456 Рік тому +1

      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 6 місяців тому

      hey is it same for longest subarray sum

  • @chethanprabhu4475
    @chethanprabhu4475 Рік тому +110

    Couple of years back, I had watched the best video on UA-cam(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 :)

  • @rpanda_old
    @rpanda_old Рік тому +22

    weird how this explanation of kadans algo is so simple compared to other yt videos. short algo short code. superb

  • @manipandit18
    @manipandit18 Рік тому +40

    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!!!.

  • @rishabh1S
    @rishabh1S Рік тому +116

    Always ready for Dsa ✅

  • @shubhamagarwal1434
    @shubhamagarwal1434 5 місяців тому +7

    #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....

  • @tarunsingh4413
    @tarunsingh4413 Рік тому +12

    at 15:22 you have to add this code in for loop . if(maxi

    • @pradeepsahu5500
      @pradeepsahu5500 4 місяці тому +1

      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

  • @vaishnaviganseh2884
    @vaishnaviganseh2884 Рік тому +4

    BEST Kadane's algo video on the internet!

  • @arnavjain762
    @arnavjain762 11 місяців тому +2

    Complete concept clarity in 20 mins. Amazing ✅✅✅✅

  • @Raj10185
    @Raj10185 Рік тому +7

    Printing the subarrays part is something i learn this time tysm understood:)

  • @Manishgupta200
    @Manishgupta200 Рік тому +6

    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))

  • @mrdevil8419
    @mrdevil8419 7 місяців тому

    🎯 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

  • @3rd_Eye_Gang
    @3rd_Eye_Gang Рік тому +23

    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 Рік тому +1

    "Understood " superb intuition of algorithm !! awsome explanation i request everyone whoever watching strivers vedeos do like and comment!!!

  • @HansrajSinghShekhawat-s3u
    @HansrajSinghShekhawat-s3u 20 днів тому

    The build up to the optimal and follow up question is fire.

  • @harigs72
    @harigs72 4 місяці тому +10

    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

    • @saitamathecoder1386
      @saitamathecoder1386 6 днів тому +1

      You must be smart

    • @harigs72
      @harigs72 5 днів тому

      @saitamathecoder1386 😂

    • @harigs72
      @harigs72 5 днів тому

      @@saitamathecoder1386It is really easy .if you had gone through the array by adding number , you would have found it out easily

  • @chaithrac370
    @chaithrac370 7 місяців тому +2

    Really amazed by the effort you put into making us understand. Thank you, Striver!

  • @yelururao1
    @yelururao1 Рік тому

    i saw many videos but not able to understad.... this video gave me complete understading..Thanks bro

  • @radhakishoriiiiiiiiiii7348
    @radhakishoriiiiiiiiiii7348 Рік тому

    12:28
    very nice explaination. Very helpful walk through that cleared my confusions

  • @RaviKumar-sn6tu
    @RaviKumar-sn6tu 9 місяців тому

    in love with kadane algorithm...all thanks to you bhaiya

  • @SydMohan
    @SydMohan Рік тому +1

    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!!

  • @cinime
    @cinime Рік тому +2

    Understood! Super excellent explanation as always, thank you very very much for your effort!!

  • @tasneemayham974
    @tasneemayham974 Рік тому +1

    BEST TEACHERRRR EVERR!!!!!!!!!!

  • @Ritik7_77
    @Ritik7_77 4 місяці тому +1

    Great teaching by great teacher ❤

  • @yugamsaini8761
    @yugamsaini8761 Рік тому +14

    Bro its 2pm night in India, You are doing great Job, consistency 💥

    • @rohitprasad5708
      @rohitprasad5708 Рік тому +1

      He is in warsaw, which means he uploaded the video between 9:30-10 pm in his time

    • @takeUforward
      @takeUforward  Рік тому +28

      Yes I could not do it during the morning today. Came back from office and recorded, edited and uploaded 😄

    • @uncannyroaches5933
      @uncannyroaches5933 Рік тому +1

      Am hai🤭🤭🤭

    • @rohitprasad5708
      @rohitprasad5708 Рік тому +1

      @@takeUforward Thats why your content is the best

  • @stith_pragya
    @stith_pragya 7 місяців тому +2

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

  • @manjamalaimm6178
    @manjamalaimm6178 10 місяців тому

    Thank You so much for made this crystal clear understanding about this problem.

  • @SurajGupta-gc9tz
    @SurajGupta-gc9tz Рік тому

    i was very keen about learning DSA and your sheet and your explanation has boosted this thank you strive bhaiya

  • @dhanviakash726
    @dhanviakash726 Місяць тому

    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

  • @allegragarland9156
    @allegragarland9156 Рік тому

    Im new to programming and this was very helpful ~

  • @syedFAHIM-el1wr
    @syedFAHIM-el1wr Рік тому +1

    Crystal Clear Understanding !

  • @kikicode-g5v
    @kikicode-g5v Рік тому +7

    15:05 editing mistake 🫡 but no worries

    • @takeUforward
      @takeUforward  Рік тому +2

      shit yes, thankfully not a big one

    • @vish-sw9dc
      @vish-sw9dc Рік тому

      ​@@takeUforward please make a video on long pressed name

    • @Poojithaalam
      @Poojithaalam 10 місяців тому

      @@takeUforward which tool you are using for white boarding?

  • @hareshnayak7302
    @hareshnayak7302 10 місяців тому +1

    Understood,Thank striver for this amazing video.

  • @embarrassed_dodo
    @embarrassed_dodo 6 місяців тому

    Very detailed explanation, it is language agnostic as well, thanks for the video

  • @AleksandrStrizhevskiy
    @AleksandrStrizhevskiy 2 місяці тому

    Thank you for this video, great explanation! I will need it for my exam in an hour haha!

  • @factfortune2160
    @factfortune2160 Рік тому

    Easiest explaination ever👌👌 thanks bhaiya...!!

  • @KarthikAsam-q3r
    @KarthikAsam-q3r Рік тому

    bro i really love your explanation ;how ever i explane doudtes to my frnds you explaining in same manner ❤

  • @sahanagoudar1647
    @sahanagoudar1647 Рік тому +1

    13:57
    Don't carry any negatives into future 😇

  • @archanaverma8771
    @archanaverma8771 Рік тому

    Your way of explanation is really outstanding🔥🔥🙌thanks lot and more!!!!

  • @ArunKumar-zp8cp
    @ArunKumar-zp8cp Рік тому

    Bro really you have good knowledge of DSA...

  • @sparshverma4030
    @sparshverma4030 Рік тому

    understood you are the best teacher. 🙌

  • @PriyanshuKumar-zd1lq
    @PriyanshuKumar-zd1lq Рік тому

    Super explanation with so much love 😃

  • @sahadevbhaganagare4761
    @sahadevbhaganagare4761 Рік тому +34

    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 9 місяців тому +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 6 місяців тому +13

      no it will return the biggest -ve number if there are all -ve in array

    • @Mythri333
      @Mythri333 2 місяці тому +1

      if all elements are negative it returning the biggest negative value because even we keep sum=0 when sum if(maxi

  • @samuelfrank1369
    @samuelfrank1369 Рік тому

    Understood. Thanks a lot. Please upload more videos Bhaiyaaa.

  • @suyashshinde2971
    @suyashshinde2971 Рік тому +2

    SDE Sheet Day 1 Problem 1 Done!

  • @VarunKaushal-zx9zq
    @VarunKaushal-zx9zq Рік тому

    Understood, Amazing Lecture Sir!

  • @swagnikdhar6010
    @swagnikdhar6010 Рік тому +1

    Absolutely Amazing ✌️🔥

  • @prabalsharma3007
    @prabalsharma3007 Рік тому

    great concepts , understood everything

  • @aryanmaniyar3475
    @aryanmaniyar3475 Рік тому

    Tired of commenting AMAZING bhaiya ;) !! You're tooooo good :)

  • @hengenaavu5737
    @hengenaavu5737 4 місяці тому

    Superb bro, exceptional 🎉🎉🎉

  • @piaknow3881
    @piaknow3881 Рік тому +1

    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

  • @sohilpathan9006
    @sohilpathan9006 Рік тому +2

    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;
    }

  • @aslamcodes
    @aslamcodes Рік тому

    8:20 "Do not worry" ✨

  • @theboredtutor
    @theboredtutor 27 днів тому

    Thanks for this, super helpful!

  • @saumyapandey8940
    @saumyapandey8940 7 місяців тому

    your course is too good

  • @Sharuthi727
    @Sharuthi727 4 місяці тому

    Thank you striver your videos are helping alot

  • @vinethasuresh3488
    @vinethasuresh3488 Рік тому +4

    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.

  • @Ashishvatsav
    @Ashishvatsav 10 місяців тому

    Amazing!! Keep going bro⚡

  • @tulsilukhi1182
    @tulsilukhi1182 8 місяців тому

    Best explanation everr

  • @AmanPandey-bd1sj
    @AmanPandey-bd1sj Рік тому

    Thanks brother Best Explanation😊

  • @JamieFord0109
    @JamieFord0109 2 місяці тому

    Understood. Thanks Striverr!!1

  • @StudyEmail-p9u
    @StudyEmail-p9u 11 місяців тому +1

    Understood!.Thank you.

  • @iitmotivationwithrahullson5930

    you are doing great job striver ❤.. .

  • @manojkumarparuchuri5920
    @manojkumarparuchuri5920 2 місяці тому

    excellent explanation thanku so much

  • @mayurkumbhar2729
    @mayurkumbhar2729 Місяць тому

    Great explanation 🎉

  • @hunter-js8hy
    @hunter-js8hy 7 місяців тому

    done and dusted ! hats off to striver ..

  • @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!

  • @Bigg_boss_trolls
    @Bigg_boss_trolls 2 місяці тому

    Nice man good teaching EXELLENT

  • @tamilmukbang3789
    @tamilmukbang3789 9 місяців тому

    Understood.. thank you so much bro

  • @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

  • @shivamsingh-we7ek
    @shivamsingh-we7ek Рік тому +2

    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 ♥♥

  • @akshatgupta1728
    @akshatgupta1728 6 місяців тому +2

    sir i know its weird to ask but sir i want to ask you that we just learned the method by watching lectures cause i don't know about kadane's algorithm and more such type of methods, or we have to practice it without watching the video .pls reply what is better to do.

  • @AyushSharma-ye1xz
    @AyushSharma-ye1xz Рік тому

    All videos are very helpful

  • @amitmauryarbl2602
    @amitmauryarbl2602 Місяць тому

    Thank you bade bhaiya

  • @manansarraf73
    @manansarraf73 5 місяців тому

    Nice explanation bhaiya

  • @rajatverma3692
    @rajatverma3692 Рік тому +1

    14:00 rule of life and rule for Kadane

  • @YerramArun
    @YerramArun Рік тому

    Understood ❤bhaiya❤❤

  • @rudramohanmishra4578
    @rudramohanmishra4578 Рік тому +2

    Hi ,
    I am unclear why O(n^3) solution is needed even in brute force solution when it can be done in O(n^2) solution , sum can be found out in the 2nd loop itself.
    If you read the post please clear the doubt.
    thanks

    • @Piyush-yp2po
      @Piyush-yp2po Рік тому

      Yes it can be done in n², declare sum variable after i loop, before j loop, while adding elmts in sum in the j loop, everytime keep updating max, once j loop finishes, sum will be back to zero

  • @suryansh70
    @suryansh70 27 днів тому

    perfect explaination.

  • @EC20022ELAKKIYAC
    @EC20022ELAKKIYAC Рік тому

    Completely Understood!

  • @prateekmahajan5490
    @prateekmahajan5490 9 місяців тому

    Understood. Thanks a ton 😇

  • @helloworld004
    @helloworld004 Рік тому +2

    class Solution {
    public:
    int maxSubArray(vector& nums) {
    int maxi=nums[0];
    int sum=nums[0];
    for(int i=1;i

  • @_hulk748
    @_hulk748 Рік тому

    UNDERSTOOD SIR🙇‍♂❤🙏

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 Місяць тому

    NICE SUPER EXCELLENT MOTIVATED

  • @stutidutta3922
    @stutidutta3922 Рік тому

    Thanks for explanation

  • @rajindersharma4
    @rajindersharma4 Рік тому

    Understood Boss, it helps !!

  • @SriSarthak257
    @SriSarthak257 8 місяців тому

    Understood striver! 🔥👍

  • @ITSuyashTiwari
    @ITSuyashTiwari Рік тому

    simply , loved it....

  • @isheep9025
    @isheep9025 Рік тому +1

    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

  • @UECAshutoshKumar
    @UECAshutoshKumar 9 місяців тому +2

    Thank you 🙏

  • @iamaniket9
    @iamaniket9 11 місяців тому

    understood ,thnx for the video ❤❤❤❤❤❤

  • @ayushmandliya9493
    @ayushmandliya9493 Рік тому +3

    #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!

  • @navkaransinghbrar4380
    @navkaransinghbrar4380 Рік тому

    understood
    // first time bro

  • @konankikeerthi
    @konankikeerthi 7 місяців тому

    Understood bro. Thank you

  • @lofiboy7866
    @lofiboy7866 3 місяці тому +1

    In interview can we give direct optimal solutions only ? Please tell anyone here

  • @francksgenlecroyant
    @francksgenlecroyant Рік тому

    Thanks bro, just subbed to the channel

  • @vaishalidhokchawle4384
    @vaishalidhokchawle4384 9 місяців тому

    Great job👍🏻