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

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ก.ย. 2024

ความคิดเห็น • 457

  • @takeUforward
    @takeUforward  ปีที่แล้ว +26

    Please watch our new video on the same topic: th-cam.com/video/AHZpyENo7k4/w-d-xo.html

    • @whateverittakes5340
      @whateverittakes5340 2 หลายเดือนก่อน +4

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

  • @takeUforward
    @takeUforward  ปีที่แล้ว +116

    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 2 หลายเดือนก่อน

      hey is it same for longest subarray sum

  • @lavanyam3224
    @lavanyam3224 7 หลายเดือนก่อน +188

    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❤

  • @rpanda_old
    @rpanda_old ปีที่แล้ว +15

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

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

  • @manipandit18
    @manipandit18 ปีที่แล้ว +33

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

  • @tarunsingh4413
    @tarunsingh4413 8 หลายเดือนก่อน +9

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

    • @pradeepsahu5500
      @pradeepsahu5500 20 วันที่ผ่านมา +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

  • @arnavjain762
    @arnavjain762 7 หลายเดือนก่อน +1

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

  • @cinime
    @cinime ปีที่แล้ว +2

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

  • @suyashshinde2971
    @suyashshinde2971 ปีที่แล้ว +2

    SDE Sheet Day 1 Problem 1 Done!

  • @manjamalaimm6178
    @manjamalaimm6178 7 หลายเดือนก่อน

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

  • @factfortune2160
    @factfortune2160 8 หลายเดือนก่อน

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

  • @archanaverma8771
    @archanaverma8771 ปีที่แล้ว

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

  • @kikicode-g5v
    @kikicode-g5v ปีที่แล้ว +6

    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 6 หลายเดือนก่อน

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

  • @hengenaavu5737
    @hengenaavu5737 หลายเดือนก่อน

    Superb bro, exceptional 🎉🎉🎉

  • @ishwardhande1847
    @ishwardhande1847 3 หลายเดือนก่อน +1

    Thanks a Lot

  • @saumyapandey8940
    @saumyapandey8940 3 หลายเดือนก่อน

    your course is too good

  • @swagnikdhar6010
    @swagnikdhar6010 ปีที่แล้ว +1

    Absolutely Amazing ✌️🔥

  • @hareshnayak7302
    @hareshnayak7302 6 หลายเดือนก่อน

    Understood,Thank striver for this amazing video.

  • @mohitsingh13
    @mohitsingh13 2 หลายเดือนก่อน

    Understood ❤

  • @HimanshuKhandelwal-rs3hn
    @HimanshuKhandelwal-rs3hn 2 หลายเดือนก่อน

    super understood

  • @U2011-n7w
    @U2011-n7w 2 หลายเดือนก่อน +1

    best video

  • @aryanmaniyar3475
    @aryanmaniyar3475 ปีที่แล้ว

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

  • @LakshminarayanChatla-gz7eh
    @LakshminarayanChatla-gz7eh 20 วันที่ผ่านมา

    Understood!

  • @gautamsaxena4647
    @gautamsaxena4647 15 วันที่ผ่านมา

    understood bhaiya

  • @prabalsharma3007
    @prabalsharma3007 10 หลายเดือนก่อน

    great concepts , understood everything

  • @SriSarthak257
    @SriSarthak257 4 หลายเดือนก่อน

    Understood striver! 🔥👍

  • @sanketh768
    @sanketh768 ปีที่แล้ว +1

    understood

  • @konankikeerthi
    @konankikeerthi 4 หลายเดือนก่อน

    Understood bro. Thank you

  • @EC20022ELAKKIYAC
    @EC20022ELAKKIYAC 8 หลายเดือนก่อน

    Completely Understood!

  • @thesyncocity6366
    @thesyncocity6366 หลายเดือนก่อน

    understood bhiaaaa!!!!

  • @satyamkumar5704
    @satyamkumar5704 2 หลายเดือนก่อน

    Understoood

  • @adityaparkhe5121
    @adityaparkhe5121 หลายเดือนก่อน

    You the goat

  • @ADITYASINGH-b4u
    @ADITYASINGH-b4u 2 หลายเดือนก่อน

    UNDERSTOOD !!

  • @ashishdhal4614
    @ashishdhal4614 ปีที่แล้ว

    Can't wait for binary search series

  • @lavanyaan2158
    @lavanyaan2158 3 หลายเดือนก่อน

    Thank you so much for the video

  • @paraschinchalkar1599
    @paraschinchalkar1599 2 หลายเดือนก่อน

    amazing video sir

  • @_hulk748
    @_hulk748 ปีที่แล้ว

    UNDERSTOOD SIR🙇‍♂❤🙏

  • @Corvo1837
    @Corvo1837 8 วันที่ผ่านมา

    Understood

  • @YerramArun
    @YerramArun ปีที่แล้ว

    Understood ❤bhaiya❤❤

  • @AyushSharma-ye1xz
    @AyushSharma-ye1xz ปีที่แล้ว

    All videos are very helpful

  • @rajatverma3692
    @rajatverma3692 ปีที่แล้ว +1

    14:00 rule of life and rule for Kadane

  • @tarishigeetey5430
    @tarishigeetey5430 2 หลายเดือนก่อน

    understood !!

  • @AniketKumar-hf2bo
    @AniketKumar-hf2bo 8 หลายเดือนก่อน

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

  • @dasaridharani6529
    @dasaridharani6529 ปีที่แล้ว

    Incredible 🎉

  • @reddygopichand2002
    @reddygopichand2002 11 หลายเดือนก่อน

    Understood thanks 🙏

  • @adebisisheriff159
    @adebisisheriff159 10 หลายเดือนก่อน

    Thanks so much striver

  • @ABISHEK-r7k
    @ABISHEK-r7k 6 หลายเดือนก่อน

    UNDERSTOOD SIR

  • @ItsAbhiDestiny
    @ItsAbhiDestiny 3 หลายเดือนก่อน

    Array Lec2(medium) Q.5 solution :----------------------------------------------------------------------------------------------------------
    class Solution {

    public static long pairWithMaxSum(long arr[], long N)
    {
    long maxSum = Long.MIN_VALUE;

    for (int i = 0; i < N - 1; i++) {
    long currentSum = arr[i] + arr[i + 1];
    if (currentSum > maxSum) {
    maxSum = currentSum;
    }
    }

    return maxSum;
    }
    }

  • @navkaransinghbrar4380
    @navkaransinghbrar4380 ปีที่แล้ว

    understood
    // first time bro

  • @priyeshtandel2101
    @priyeshtandel2101 ปีที่แล้ว

    Understood

  • @francksgenlecroyant
    @francksgenlecroyant ปีที่แล้ว

    Thanks bro, just subbed to the channel

  • @nikhilmolugu3777
    @nikhilmolugu3777 หลายเดือนก่อน

    understood

  • @PankajSingh-pb4vs
    @PankajSingh-pb4vs 7 หลายเดือนก่อน

    Amazing ❤

  • @JitendraKumar-ll4lz
    @JitendraKumar-ll4lz ปีที่แล้ว

    Amazing experience

  • @nishthaarora1907
    @nishthaarora1907 11 หลายเดือนก่อน

    but if the subarray consist of on;y negative elements , then in that case max sum should be the smallest negative element in the array ?

  • @anirudhrana5578
    @anirudhrana5578 3 หลายเดือนก่อน

    Striver 🔥🔥🔥🔥🔥

  • @programmingsoul4269
    @programmingsoul4269 ปีที่แล้ว

    Thanks bhaiya 💖💖

  • @naveenkumarchukka4280
    @naveenkumarchukka4280 ปีที่แล้ว

    Thank you!

  • @jackfrost8969
    @jackfrost8969 6 หลายเดือนก่อน

    def maxSubArray(nums):
    max_sum = nums[0]
    current_sum = nums[0]

    for num in nums[1:]:
    current_sum = max(num, current_sum + num)
    max_sum = max(max_sum, current_sum)

    return max_sum
    nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
    print(maxSubArray(nums))

  • @shaiksoofi3741
    @shaiksoofi3741 8 หลายเดือนก่อน

    TQ

  • @sukhpreetsingh5200
    @sukhpreetsingh5200 ปีที่แล้ว +1

    Awesome

  • @suresmishra-5593
    @suresmishra-5593 ปีที่แล้ว

    You the best❤

  • @curs3m4rk
    @curs3m4rk 6 หลายเดือนก่อน

    i understood the algo and intuition as well, but after some time I am forgetting things. what shall I do

    • @striver17
      @striver17 6 หลายเดือนก่อน

      I feel the same! what are u doing??

    • @curs3m4rk
      @curs3m4rk 6 หลายเดือนก่อน

      @@striver17 I’m trying to revise as much as possible everyday

  • @rajgandhi8548
    @rajgandhi8548 ปีที่แล้ว

    Understood!

  • @rakshanaravindran9809
    @rakshanaravindran9809 ปีที่แล้ว

    Understood!

  • @rajeshbandaru2407
    @rajeshbandaru2407 8 หลายเดือนก่อน

    Hey Striver! How can I find the second maximum subarray sum in an array

  • @milanthakur4975
    @milanthakur4975 ปีที่แล้ว +1

  • @RiteshSingh-yp1sm
    @RiteshSingh-yp1sm ปีที่แล้ว

    thanks

  • @m.nirupreddy8001
    @m.nirupreddy8001 ปีที่แล้ว

    understood!

  • @bnmdevilop
    @bnmdevilop ปีที่แล้ว

    I understand it but I have one question like if all the elements are negative in the array then what we can do?

    • @takeUforward
      @takeUforward  ปีที่แล้ว

      The smallest number as a single element will be the subarray

    • @siddharthsinghaniya3177
      @siddharthsinghaniya3177 ปีที่แล้ว

      @@takeUforward but how to code can you please make a video , i haven't seen any video on youtube who explain about all the negative subarrays

  • @per.seus._
    @per.seus._ ปีที่แล้ว

    understood♥

  • @ishanmathur447
    @ishanmathur447 ปีที่แล้ว

    understood...

  • @ayushgupta0
    @ayushgupta0 ปีที่แล้ว

    understood.

  • @shotbotop3790
    @shotbotop3790 2 หลายเดือนก่อน

    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 2 หลายเดือนก่อน

      I know it should not work but idk why its working on lc?😅

    • @graviton001
      @graviton001 2 หลายเดือนก่อน

      I added the test case -1 from my side then also its working but it should return 0 LOL.

  • @Shivi32590
    @Shivi32590 3 หลายเดือนก่อน

    LeetCode JAVA Solution:
    class Solution {
    public int maxSubArray(int[] nums) {
    int max= Integer. MIN_VALUE;
    int sum=0;
    int c=Integer.MIN_VALUE;
    for(int i=0;imax)
    max=sum;
    }
    if(max==0)
    max=c;
    return max;
    }
    }

  • @sangeeta35
    @sangeeta35 ปีที่แล้ว

    i have a question , if in interview we do this question like brute force then optimal , do i need to mention the algo used , won't the interviewer think why i started with brute when i knew this algo need to be used?

    • @takeUforward
      @takeUforward  ปีที่แล้ว

      Usually they don't ask these questions, they want to check your solving skills. You can also tell about the algorithm without telling the name..

    • @sangeeta35
      @sangeeta35 ปีที่แล้ว

      @@takeUforward thank you ✨

  • @philosphize
    @philosphize ปีที่แล้ว

    A2Z series is the Gold

  • @chethanprabhu4475
    @chethanprabhu4475 ปีที่แล้ว +100

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

  • @SYCOA12CHAITANYAASOLE
    @SYCOA12CHAITANYAASOLE 4 หลายเดือนก่อน

    Understood !!

  • @arnavkumar4631
    @arnavkumar4631 หลายเดือนก่อน

    Understood

  • @alia_sd_2781
    @alia_sd_2781 ปีที่แล้ว

    Understood

  • @KotgireTejas
    @KotgireTejas 2 หลายเดือนก่อน

    understood

  • @ashishpradhan6250
    @ashishpradhan6250 ปีที่แล้ว

    understood

  • @Sanjay-v7p8q
    @Sanjay-v7p8q ปีที่แล้ว

  • @rishabh1S
    @rishabh1S ปีที่แล้ว +105

    Always ready for Dsa ✅

  • @harigs72
    @harigs72 24 วันที่ผ่านมา +3

    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

  • @Raj10185
    @Raj10185 ปีที่แล้ว +6

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

  • @vinethasuresh3488
    @vinethasuresh3488 ปีที่แล้ว +5

    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.

  • @akshatgupta1728
    @akshatgupta1728 2 หลายเดือนก่อน +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.

  • @parthsalat
    @parthsalat 4 หลายเดือนก่อน +2

    Are you using an iPad or something else?

  • @komalyadav5599
    @komalyadav5599 หลายเดือนก่อน

    Understood

  • @yugamsaini8761
    @yugamsaini8761 ปีที่แล้ว +11

    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  ปีที่แล้ว +22

      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 ปีที่แล้ว +1

      @@takeUforward Thats why your content is the best

  • @GungunRamchandani
    @GungunRamchandani 2 หลายเดือนก่อน

    Understood

  • @RajNamdev_19
    @RajNamdev_19 3 หลายเดือนก่อน

    Understood

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

  • @BtechNibba
    @BtechNibba 4 หลายเดือนก่อน

    Understood

  • @abhishekahirvar7783
    @abhishekahirvar7783 6 หลายเดือนก่อน

    Understood

  • @abhishekprasad010
    @abhishekprasad010 7 หลายเดือนก่อน

    Understood

  • @abhaygoyal594
    @abhaygoyal594 8 หลายเดือนก่อน

    Understood