Kadane's Algorithm - Maximum Sum Subarray (Amazon Coding Interview Question)

Поделиться
HTML-код
  • Опубликовано: 12 дек 2024

Комментарии • 295

  • @chrishamilton1728
    @chrishamilton1728 4 года назад +389

    Samsung: "Try to find the maximum sub-arr-"
    Me after watching Nick White: "Yo, you're coming at me with this weak crap?"

  • @kevintran7085
    @kevintran7085 4 года назад +17

    Hey Nick, I watched all of your LeetCoding videos to help me prepare for my technical interviews. I was able to secure two offers from big tech companies (including a FAANG). I just wanted to thank you for making these videos and helping people like myself achieve their professional goals!

    • @abhi.r8
      @abhi.r8 3 года назад +1

      He teaches in which language and u wrote code in interviews in which language ??

    • @g.deepakkrishnaa3847
      @g.deepakkrishnaa3847 3 года назад +1

      @@abhi.r8 he teaches in JAVA and I don't know about the second question

  • @kimsung2384
    @kimsung2384 4 года назад +47

    This guy rocks! I love his videos and explanations. I’ve been in the programming space for 21.5 years but I don’t get bored of listening to Nick White. He’s intelligent, enthusiastic, innovative and has excellent problem solving skills. This is the guy I’d like in my team.

  • @tanishktripathi8773
    @tanishktripathi8773 4 года назад +14

    Hey Nick!
    Many students from India just like me watch your channel and learn logics with very easily readable code.
    I just want to tell you that you are doing a great job and please keep the videos coming Everybody enjoys your video not just because you solve the problem but because you solve them with a lot of ease and fun.

  • @krishcan4727
    @krishcan4727 4 года назад +35

    The actual kandane algorithm explanation starts at 6:50

  • @gergerger53
    @gergerger53 2 года назад

    When you were winding down with the wrap-up, that's usually when I close the videos and I didn't really appreciate the algorithm, but I was so happy to have it running in the background as you revisited it and then demonstrated why it works and that just cleared all the fog and it suddenly made perfect sense.

  • @raj22kesh
    @raj22kesh 3 года назад

    I like this Nick White's explanation. He is precise simple and short. He is not boring explanation like others who takes 30min explanation on every small topics. When you are on the flow and need some hints. You wouldn't like to listen to 30-40 min boring explanation.

  • @quinn479
    @quinn479 4 года назад +253

    Fun fact - Jay Kadane came up with this algorithm in under a minute after other researchers spent months working on different solutions.

    • @daruiraikage
      @daruiraikage 3 года назад +42

      that a lie innit, he well nicked the idea off of me uncle jamaal

    • @rahulbalan
      @rahulbalan 3 года назад +33

      Those other researchers is me.

    • @ButerWarrior44
      @ButerWarrior44 3 года назад

      wow what a nerd. shit took me 3 hours 🤓🤓

    • @Nivek4g
      @Nivek4g 3 года назад +66

      Researchers spent months while interviewers expect 30 minutes lmao.

    • @saurabhraj5772
      @saurabhraj5772 2 года назад +10

      But the irony is interviewer expect in 25 minutes with production ready code. So if you don't know algorithm in advance then very difficult to crack and if you know then anyone can solve in 10 minutes.

  • @mgtowindia9549
    @mgtowindia9549 4 года назад +48

    I like to watch your videos while taking dump every day at 1 pm as it helps to build the pressure in my abdomen.

  • @Luna-vk9xy
    @Luna-vk9xy 4 года назад +3

    I've been trying to find a proper explaination for this algorithm for a day now and I finally understand how it works. Thank you so much

  • @pauls60r
    @pauls60r 4 года назад +1

    This is the clearest explanation of Kadane's Algo I've seen thus far

  • @jamesyoo67
    @jamesyoo67 4 года назад +23

    This is the BEST channel for learning algos and interviewing .

  • @ashwinishanbhogue2917
    @ashwinishanbhogue2917 4 года назад +6

    Your videos are the best! I’m preparing for a job switch and learning ds & algorithms, nobody explains the things like you do. Thank you so much for helping us out! Keep doing the good work!

  • @putthetrust
    @putthetrust 4 года назад +1

    People should be liking your videos a lot because you are putting this content out for free... Thanks for all the help

  • @lokeshrajselvaraj5288
    @lokeshrajselvaraj5288 2 года назад +1

    NIT - Magic at 12:58. Line#6 , cur became current_sum on click run tests. Just for fun (or i did not understand your sarcasm). Your videos rock.
    Going back and explaining how we lost -2 and started over with 2 is amazing.

  • @brah2K
    @brah2K 4 года назад +148

    12:56 “people will say...” cuts the video and corrects the variable name lol

    • @NickWhite
      @NickWhite  4 года назад +27

      hahahahaha

    • @Frederic_Chopin.
      @Frederic_Chopin. 4 года назад

      Lmao

    • @josiahroa177
      @josiahroa177 4 года назад +7

      Was wondering what was gonna happen when he went to run the code lol

    • @MariyamVlogs707
      @MariyamVlogs707 4 года назад +1

      @@NickWhite use a visual platform how each line of code executes programme because many are suffering how logic internally works

    • @punstress
      @punstress 4 года назад +1

      Which variable, I don't see it?

  • @okechukwuobi3611
    @okechukwuobi3611 2 года назад

    I have not watch your video for more than 2 min and you nailed it with a good explanation , you are the best

  • @MikeLeed
    @MikeLeed 2 года назад

    I was on the fence about giving that like he asked for, but the little plug in 5:38 cemented it, liked from me, all day everyday, fuck yea for Nick White.

  • @yiyaoran8637
    @yiyaoran8637 9 месяцев назад

    I spent 7 hours (whole night) reading the textbook and watching RUclips trying to understand this. Was about to give up my college until i saw this video.

  • @davidjames1684
    @davidjames1684 3 года назад

    One "preprocessing step" would be to throw away any leading and trailing negative numbers in the array since they cannot help, then apply Kadane's algorithm to whatever is left. So the solution to [-2, 2, 5, -11, 6] is the same as the solution to [2, 5, -11, 6] which is the same as the solution to [-4, -3, -2, 2, 5, -11, 6, -1, -99]

  • @asishbalu9771
    @asishbalu9771 4 года назад +4

    Finally someone with great technical skills AND communication skills on youtube!!

  • @kushalbaldev8490
    @kushalbaldev8490 4 года назад

    Hope that helps more to understand just added comment to the code to dry track.!!
    int [] array= {-2,2,5,-11,6};
    int max_sum=array[0]; //i.e -2
    int current_sum=max_sum; //i.e -2
    for(int i=1;i

  • @mariyaburrows2692
    @mariyaburrows2692 3 года назад

    Honestly, this is the best explanation I've seen on Kadane's. Thank you!

  • @yakinwissem8665
    @yakinwissem8665 4 года назад +13

    your content is amazing, its way too good to be true

  • @JohnSmith-xf1zu
    @JohnSmith-xf1zu 3 года назад +2

    Damn. I was going though Priority Queues similar to a Huffman encoding tree combing Max Sum pairs into new nodes and leafs, but your solution was much easier. Nice work!

  • @naveennoel9496
    @naveennoel9496 4 года назад +2

    I love the energy and confidence. It boosts up one's coding brain :)

  • @ritvikbhatia9647
    @ritvikbhatia9647 4 года назад

    This channel deserves more attention. Awesome explanation.

  • @krishnajithk9322
    @krishnajithk9322 3 года назад +1

    Your way of explaining things is awesome brother... 🔥

  • @momenbazzar
    @momenbazzar 3 года назад

    This algorithm helped me with a question I was struggling with, and now I'm able to solve it, thanks a lot.

  • @akhileshguptaakhi
    @akhileshguptaakhi 4 года назад

    very crisp and clear. Thanks. Small suggestion : I hope you organize your playlist topic wise

  • @josiahroa177
    @josiahroa177 4 года назад +5

    Dropping a like and a comment. This channel deserves so much more attention

  • @easysolutions135
    @easysolutions135 3 года назад

    Bro...!!! You deserve lot more than ...likes. I have shared your video ...with my mates.

  • @emmanueletti5475
    @emmanueletti5475 4 года назад +12

    If you ever find it hard to understand the implementation, just know that current_sum stores the sums of the Contiguous sub array. And if the sum is every less that the next element in the array, then it starts a new count.

  • @ankitsharma3490
    @ankitsharma3490 4 года назад +1

    Can we also solve it through recursion? Essentially breaking down arrays into two parts and returning max among them. Base case would be array size 1.
    1) Array including first index and excluding last index
    2) Array excluding first index and including last index

    • @swagatikachoudhury2115
      @swagatikachoudhury2115 2 года назад

      yes, I tried the same approach....It will work but I got TLE for some of the test cases.
      PFA:
      class Solution {
      public int maxSubArray(int[] nums) {

      int total=0;
      for(int i=0;i

  • @dipper0yawn
    @dipper0yawn 4 года назад

    I like your approach of showing the code last. Liked and subscribed!

  • @anuragsandhu9590
    @anuragsandhu9590 4 года назад

    Code in Javascript:
    function findMaxSubArray(ar) {
    let currentSum = ar[0], maxSum = ar[0], index = 1;
    while (index < ar.length) {
    currentSum = ar[index] < (currentSum + ar[index]) ? (currentSum + ar[index]) : ar[index];
    if (maxSum < currentSum) maxSum = currentSum;
    index++;
    }
    return maxSum;
    }

  • @pradyothcherry3426
    @pradyothcherry3426 4 года назад +1

    There is slight Correction in this code!
    Initialize:
    max_so_far = 0
    max_ending_here = 0
    Loop for each element of the array
    max_ending_here = max_ending_here + a[i]
    if(max_ending_here < 0)
    max_ending_here = 0
    if(max_so_far < max_ending_here)
    max_so_far = max_ending_here
    return max_so_far

  • @maythecodesbewithyou29
    @maythecodesbewithyou29 4 года назад

    kadanes is a must do question for all programmers

  • @AnkitAsatiM10
    @AnkitAsatiM10 4 года назад

    Awesome! The best explanation [and not just code walkthrough like many others] on the entire youtube!

  • @vivek2631991
    @vivek2631991 4 года назад +1

    Great!
    You should also consider trying LC# 690 (employee importance) and LC# 543 (Diameter of binary tree).

  • @gu3566
    @gu3566 2 года назад

    Thank you Nick! It’s a really clear and straight explanation.

  • @shubhamgupta7652
    @shubhamgupta7652 4 года назад

    your way of approaching problem is amazing i have learn a lot from you thanks for such amazing videos

  • @kamaleshananthapadmanabhan1573
    @kamaleshananthapadmanabhan1573 4 года назад +2

    5:21 Shouldn't it be O(N^3) because you are taking the sum of the subarrays?

  • @antianti4331
    @antianti4331 2 года назад

    Nice representation of dynamic programming

  • @ByteMock
    @ByteMock 4 года назад +3

    Great question - we will ask this one soon. Thanks for the idea!

  • @MozwGamer
    @MozwGamer 2 года назад

    If we run two loops, one for the first and one for the last number, and then we sum. So we start to compare which one is greater if we offset the first or the last (closing in).
    Edit: I'm new to coding, I just came here to see which problems are expected to be solved.

  • @streamcoders3552
    @streamcoders3552 3 года назад

    Learning during ICPC prelimary contest .. Thanks Nick !

  • @samandarboymurodov8941
    @samandarboymurodov8941 3 года назад +1

    thank you, Nick. It is very well explained

  • @luckyismynickname
    @luckyismynickname 4 года назад +3

    Amazing man please please do more videos like that. I appreciate your hard work on this. Keep growing brother 😊😊😊

  • @anonimettalontana4944
    @anonimettalontana4944 2 года назад

    Liking before even watching! Thanks, man!

  • @anhhaopham7919
    @anhhaopham7919 3 года назад

    thank you so much, I'm a newbie, this video helped me understand easy than the others

  • @kayveekhatrao2486
    @kayveekhatrao2486 4 года назад +1

    Your explanation was 👌👌😍

  • @lazyto12
    @lazyto12 2 года назад +1

    Solution method/function is off. Try input array = {-2,2,5,-11,8,6,-7,10}. At 7th iterating;- current_sum = max(current_sum + arr[7], arr[7]). the result is current_sum + 10 = 14 + 10 = 24. Result should be 14

    • @seanli188
      @seanli188 6 месяцев назад

      No, it is not, according to the algorithm, your current sum at 7th iteration is 8+6+-7 which is 7, 7+10 is 17 which is the new Max sum

  • @deadelinor
    @deadelinor 3 года назад

    I tried watching 2-3 other videos but my dumbass brain could only understand this explanation. Great job!

  • @spaghettipupper9901
    @spaghettipupper9901 3 года назад

    Lol I like how right before he runs to code to show that it works he jump cuts to it passing after he changed cur to current_sum at 12:55

  • @baubekukibassov7361
    @baubekukibassov7361 4 года назад

    Cur

  • @brandonquach2550
    @brandonquach2550 3 года назад

    Window that keeps moving right till current is less than 0. Then set start to right + 1

  • @vasujain1970
    @vasujain1970 4 года назад

    Your videos are really good and useful! Keep it up and don't stop posting!

  • @madhuj6912
    @madhuj6912 2 года назад

    Absolutely Brilliant!! Well explained!! Thanks a lot for your contribution.

  • @ABHIRAMR1
    @ABHIRAMR1 3 года назад

    Amazing explanation 🤩🔥🔥🔥

  • @umamaheshsukamanchi
    @umamaheshsukamanchi 3 года назад +1

    Great explanation

  • @advithn5990
    @advithn5990 2 года назад

    This was amazing ... Thanks Nick

  • @expansivegymnast1020
    @expansivegymnast1020 2 года назад

    I got this problem in my Amazon internship OA. Totally messed it up. Learned from my mistake.

  • @satyabusi7344
    @satyabusi7344 4 года назад +1

    Thanks Nick. Appreciate the help.

  • @yashp1nth
    @yashp1nth 4 года назад +1

    According to wikipedia Mr.Kadane came up with the algorithm in less than a minute

  • @sameer9368
    @sameer9368 4 года назад

    You are the best the way you explain the problem

  • @MuhammadAhsan-hq2bc
    @MuhammadAhsan-hq2bc 4 года назад

    i hit the like button for my man ; Nick White!

  • @rchukkapalli1
    @rchukkapalli1 4 года назад +1

    Watch a lot of videos and will do "like". you are doing an amazing job and I am learning a lot.

  • @louis-hz4up
    @louis-hz4up 4 года назад +2

    Hi Nick, i have a short Question: Is the Steem Account "iwudah" belongs to you and do you have recently published this Video here on the Steem Platform? Thanks in Advance - greetings, louis

  • @MrTomro
    @MrTomro 4 года назад

    I hope your channel gets more popular, this is top quality content. Also just a thing i noticed, the statement with x = max(a, a+b) is way cleaner and shorter than what I would typically write like an if and else statement 👍

    • @franksmith80
      @franksmith80 3 года назад

      Agreed, I noticed that too and thought wow, that's way cleaner than I would've done that lol

  • @justaguy2247
    @justaguy2247 4 года назад +69

    Gave you a like just to help, and this comment is also for the algorithm.

  • @karnamkalpesh1988
    @karnamkalpesh1988 4 года назад

    Man nice videos, it helped me understand the concept in very east manner

  • @folklinoff
    @folklinoff 4 года назад

    I like the explanation at all, but there is a slight logic improvement that can be made in line 5. Notice that we check for the greatest of nums[i] and nums[i]+ current_sum. It is the same as comparing nums[i] + 0 and nums[I] + current_sum. Substracting nums[i] from both sides gives us comparing 0 and current_sum. And our current_sum then must look like Max(0, current_sum) + nums[i]. Not that much of work, but looks a bit more perfect as for me. Still great video as always ❤

  • @jjchoi08
    @jjchoi08 4 года назад +1

    Thanks for this video! Is knowing bruteforce and Kane's aglo enough for this problem? Would interviewers usually ask to solve this in different approach like divide and conquer?

  • @sitimartoon2521
    @sitimartoon2521 2 года назад

    Good Screening Explanation...Keep it up

  • @tathagatnegi5923
    @tathagatnegi5923 4 года назад +2

    Sir can you make a video on how to calculate time and space complexity . Like i have seen many videos but still. For ex- take 1 example of all sorting techniques and tell us how to find time complexity and you can even modify the sorting technique and show us the new complexity....it will be of great help sir🥺

  • @dibyajyotisarkar5288
    @dibyajyotisarkar5288 4 года назад +2

    Wow! Thank you very much for the detailed explanation it really helped.

  • @MrAdy0207
    @MrAdy0207 4 года назад

    5:52 for O(n)

  • @NadyaPena-01
    @NadyaPena-01 4 года назад

    LIKE THE VIDEO, GUYS. We're obviously here learning from this video so LIKE IT.

  • @junehaoching6095
    @junehaoching6095 4 года назад +3

    Love it! I found gold when I found your channel!
    Tried with several cases, with the methods you taught, how can we return the index of the subarray as well?

    • @0x6e95
      @0x6e95 4 года назад +1

      You could try keeping two pointers m and n for returning the indices of the subarray. We would update both m and n whenever we find a cur_sum that beats our max_sum and starts a new subarray sum (i.e current element > cur_sum). We would update only n if our cur_sum beats out our max_sum and we don't start a new subarray sum.

  • @abhi4unme2002
    @abhi4unme2002 3 года назад +1

    Hey Nick you forgot to change the cur to curr_sum at line 6. Btw, nice explaination.

  • @UCS_RheaRaviSharma-fs9bs
    @UCS_RheaRaviSharma-fs9bs 4 года назад

    The best explanation ever!!!!

  • @anyagapyuk2423
    @anyagapyuk2423 3 года назад

    Good explanation. Thank you.

  • @Andrew-ut2wi
    @Andrew-ut2wi 4 года назад

    "Please hit the like button. I don't know if that'll make you wanna do it less, but... gotta try!" Hahaha. Liked!

  • @etherioussanjudraganeel3163
    @etherioussanjudraganeel3163 3 года назад

    Lol the introduction is pretty straight forward

  • @lilaznboy101
    @lilaznboy101 4 года назад +1

    Thanks for these videos, you help me get back on track when feeling unmotivated (:

  • @somewheresomeone3959
    @somewheresomeone3959 2 года назад

    Hi there, thanks for the video, but when I submit the code following the ideas, the leetcode gives me a special case, when length of the array = 1, for example, then the output will be 2, so better get the length checked before the loop.

  • @elementalneil7967
    @elementalneil7967 3 года назад +1

    He calls the O(n^2) solution brute-force. The first solution I managed to come up with was O(n^3). Fml.

  • @swaroopmaddu
    @swaroopmaddu 4 года назад

    Great explanation I have seen Thanks

  • @ashrafsiddiqui7147
    @ashrafsiddiqui7147 3 года назад

    you are a genius man.

  • @ShubhamSingh-vh1vw
    @ShubhamSingh-vh1vw 4 года назад +1

    Great work brother keep it up...

  • @tathagatnegi5923
    @tathagatnegi5923 4 года назад +4

    You are the best❤️

  • @krishna100982
    @krishna100982 4 года назад +3

    Did you apply for SDE at Amazon, seems like you know more than enough to get in.

  • @prakashkrishna2566
    @prakashkrishna2566 2 года назад

    class Solution {
    public int maxSubArray(int[] nums) {
    int curr_sum = nums[0];
    int max_sum = nums[0];

    for(int i=1;i

  • @Historyiswatching
    @Historyiswatching 2 года назад

    Cutest coding teacher on RUclipseee 💗
    I mean best. Best. ☺️

  • @kyledrewes6552
    @kyledrewes6552 4 года назад

    Hi Nick, what would be a more complicated variation of this problem that may be asked in an interview ?

  • @prathyushakadali3330
    @prathyushakadali3330 6 месяцев назад

    Hi Nick, Thanks so much for sharing this. QQ: Shouldn't max_sum be equal to zero initially?

  • @adil6873
    @adil6873 4 года назад +4

    Hey..Can you please continue your "Technical Interview Study Guide" series,it's quite helpful.

  • @smaranh
    @smaranh 4 года назад

    O(N) Time complexity where N is the number of elements in the inputArray
    O(1) Space complexity or is it O(N)
    Nick is this correct?

    • @samuelgunter
      @samuelgunter 4 года назад +2

      The space complexity of this algorithm is O(2) since there are only two integers that are stored, regardless of the input. Usually we remove the coefficients of Big O Notation, so if we do that to this the space complexity is O(1) [constant space]

  • @valentinmorales2633
    @valentinmorales2633 Год назад

    Here is your like good man, excellent explanation!