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

Sdílet
Vložit
  • čas přidán 26. 07. 2024
  • The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
    Join my free exclusive community built to empower programmers! - www.skool.com/software-develo...
    Preparing For Your Coding Interviews? Use These Resources
    --------------------
    (My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.dev/courses/nick
    AlgoCademy - algocademy.com/?referral=nick...
    Daily Coding Interview Questions - bit.ly/3xw1Sqz
    10% Off Of The Best Web Hosting! - hostinger.com/nickwhite
    Follow My Twitter - / nicholaswwhite
    Follow My Instagram - / nickwwhite
    Other Social Media
    ----------------------------------------------
    Discord - / discord
    Twitch - / nickwhitettv
    TikTok - / nickwhitetiktok
    LinkedIn - / nicholas-w-white
    Show Support
    ------------------------------------------------------------------------------
    Patreon - / nick_white
    PayPal - paypal.me/nickwwhite?locale.x...
    Become A Member - / @nickwhite
    #coding #programming #softwareengineering
  • Věda a technologie

Komentáře • 294

  • @chrishamilton1728
    @chrishamilton1728 Před 4 lety +388

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

  • @kimsung2384
    @kimsung2384 Před 4 lety +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 Před 3 lety +13

    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.

  • @ashwinishanbhogue2917
    @ashwinishanbhogue2917 Před 4 lety +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!

  • @Murphyalex
    @Murphyalex Před 2 lety

    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.

  • @Luna-vk9xy
    @Luna-vk9xy Před 4 lety +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

  • @quinn479
    @quinn479 Před 3 lety +254

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

    • @daruiraikage
      @daruiraikage Před 3 lety +42

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

    • @rahulbalan
      @rahulbalan Před 2 lety +34

      Those other researchers is me.

    • @ButerWarrior44
      @ButerWarrior44 Před 2 lety +1

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

    • @Nivek4g
      @Nivek4g Před 2 lety +66

      Researchers spent months while interviewers expect 30 minutes lmao.

    • @saurabhraj5772
      @saurabhraj5772 Před 2 lety +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.

  • @kevintran7085
    @kevintran7085 Před 4 lety +15

    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 Před 3 lety +1

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

    • @g.deepakkrishnaa3847
      @g.deepakkrishnaa3847 Před 2 lety +1

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

  • @mgtowindia9549
    @mgtowindia9549 Před 4 lety +46

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

  • @brah2K
    @brah2K Před 4 lety +148

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

    • @NickWhite
      @NickWhite  Před 4 lety +27

      hahahahaha

    • @Frederic_Chopin.
      @Frederic_Chopin. Před 4 lety

      Lmao

    • @josiahroa177
      @josiahroa177 Před 4 lety +7

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

    • @MariyamVlogs707
      @MariyamVlogs707 Před 4 lety +1

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

    • @punstress
      @punstress Před 4 lety +1

      Which variable, I don't see it?

  • @putthetrust
    @putthetrust Před 4 lety +1

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

  • @pauls60r
    @pauls60r Před 3 lety +1

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

  • @AnkitAsatiM10
    @AnkitAsatiM10 Před 3 lety

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

  • @mariyaburrows2692
    @mariyaburrows2692 Před 2 lety

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

  • @josiahroa177
    @josiahroa177 Před 4 lety +5

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

  • @naveennoel9496
    @naveennoel9496 Před 3 lety +2

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

  • @raj22kesh
    @raj22kesh Před 2 lety

    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.

  • @krishcan4727
    @krishcan4727 Před 4 lety +34

    The actual kandane algorithm explanation starts at 6:50

  • @yakinwissem8665
    @yakinwissem8665 Před 4 lety +13

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

  • @ritvikbhatia9647
    @ritvikbhatia9647 Před 4 lety

    This channel deserves more attention. Awesome explanation.

  • @jamesyoo67
    @jamesyoo67 Před 4 lety +23

    This is the BEST channel for learning algos and interviewing .

  • @ByteMock
    @ByteMock Před 4 lety +3

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

  • @JohnSmith-xf1zu
    @JohnSmith-xf1zu Před 2 lety +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!

  • @vivek2631991
    @vivek2631991 Před 4 lety +1

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

  • @okechukwuobi3611
    @okechukwuobi3611 Před 2 lety

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

  • @momenbazzar
    @momenbazzar Před 2 lety

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

  • @akhileshguptaakhi
    @akhileshguptaakhi Před 4 lety

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

  • @vasujain1970
    @vasujain1970 Před 3 lety

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

  • @satyabusi7344
    @satyabusi7344 Před 4 lety +1

    Thanks Nick. Appreciate the help.

  • @gu3566
    @gu3566 Před rokem

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

  • @neosapien247
    @neosapien247 Před 4 lety

    Awesome new vibe you got goin on. :D Thanks for the video!

  • @luckyismynickname
    @luckyismynickname Před 4 lety +3

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

  • @dibyajyotisarkar5288
    @dibyajyotisarkar5288 Před 4 lety +2

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

  • @lilaznboy101
    @lilaznboy101 Před 4 lety +1

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

  • @shubhamgupta7652
    @shubhamgupta7652 Před 4 lety

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

  • @sakethmattupalli5271
    @sakethmattupalli5271 Před 4 lety

    This was an amazing explanation!!!

  • @yiyaoran8637
    @yiyaoran8637 Před 5 měsíci

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

  • @dipper0yawn
    @dipper0yawn Před 3 lety

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

  • @rchukkapalli1
    @rchukkapalli1 Před 4 lety +1

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

  • @krishnajithk9322
    @krishnajithk9322 Před 2 lety +1

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

  • @streamcoders3552
    @streamcoders3552 Před 3 lety

    Learning during ICPC prelimary contest .. Thanks Nick !

  • @lokeshrajselvaraj5288
    @lokeshrajselvaraj5288 Před 2 lety +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.

  • @anonimettalontana4944
    @anonimettalontana4944 Před 2 lety

    Liking before even watching! Thanks, man!

  • @madhuj6912
    @madhuj6912 Před 2 lety

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

  • @konradonbal2020
    @konradonbal2020 Před 4 lety +1

    At long last! I am watching your videos for couple months and finally when I saw your new video I said: nah, i know that, no need to analyze this :D

  • @davidjames1684
    @davidjames1684 Před 3 lety

    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]

  • @advithn5990
    @advithn5990 Před 2 lety

    This was amazing ... Thanks Nick

  • @MrTomro
    @MrTomro Před 4 lety

    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 Před 3 lety

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

  • @samandarboymurodov8941
    @samandarboymurodov8941 Před 3 lety +1

    thank you, Nick. It is very well explained

  • @MikeLeed
    @MikeLeed Před 2 lety

    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.

  • @asishbalu9771
    @asishbalu9771 Před 4 lety +4

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

  • @easysolutions135
    @easysolutions135 Před 3 lety

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

  • @anhhaopham7919
    @anhhaopham7919 Před 3 lety

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

  • @prathaps2753
    @prathaps2753 Před 4 lety

    hey continue to post such videos,i love it

  • @navidr2811
    @navidr2811 Před 3 lety

    great explanation, thanks man!

  • @ShubhamSingh-vh1vw
    @ShubhamSingh-vh1vw Před 4 lety +1

    Great work brother keep it up...

  • @UCS_RheaRaviSharma-fs9bs

    The best explanation ever!!!!

  • @karnamkalpesh1988
    @karnamkalpesh1988 Před 4 lety

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

  • @deepuahmad2038
    @deepuahmad2038 Před 4 lety

    This helps, thank you very much , great job ❣️👏

  • @MuhammadAhsan-hq2bc
    @MuhammadAhsan-hq2bc Před 3 lety

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

  • @antianti4331
    @antianti4331 Před 2 lety

    Nice representation of dynamic programming

  • @TechOnScreen
    @TechOnScreen Před 2 lety

    gr8 work Nick.

  • @karansagar7870
    @karansagar7870 Před 4 lety

    Gr8 explanation bro , even ads on ur channel are great altium stories

  • @maythecodesbewithyou29

    kadanes is a must do question for all programmers

  • @MozwGamer
    @MozwGamer Před 2 lety

    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.

  • @mahmudshanto6065
    @mahmudshanto6065 Před 4 lety

    Bro, I really appreciate those videos. It would be really helpful if you come up with something about this problem 'closest pair of points on 2d plane'.

  • @junehaoching6095
    @junehaoching6095 Před 4 lety +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 Před 4 lety +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.

  • @sameer9368
    @sameer9368 Před 4 lety

    You are the best the way you explain the problem

  • @doopian
    @doopian Před 4 lety +6

    I normally lurk, but ill give you a like and post this comment since you've helped me learn a lot :)

  • @pranabsarkar1384
    @pranabsarkar1384 Před 3 lety

    Thanks bro :) more power to you!

  • @tathagatnegi5923
    @tathagatnegi5923 Před 4 lety +4

    You are the best❤️

  • @kayveekhatrao2486
    @kayveekhatrao2486 Před 4 lety +1

    Your explanation was 👌👌😍

  • @sumitroy7817
    @sumitroy7817 Před 4 lety +1

    hey your videos are really helpful.. keep up the good work, can you please create a playlist apart from "leetcode" topicwise?

  • @sandundhund
    @sandundhund Před 4 lety +1

    Could yo do a vid on betweeness centrality? Great vid!

  • @umamaheshsukamanchi
    @umamaheshsukamanchi Před 3 lety +1

    Great explanation

  • @vigyanhoon
    @vigyanhoon Před 4 lety +1

    I love your intro.

  • @emmanueletti5475
    @emmanueletti5475 Před 3 lety +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.

  • @nayyyhaa
    @nayyyhaa Před 3 lety

    LOVE THISSS💯

  • @kushalbaldev8490
    @kushalbaldev8490 Před 4 lety

    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

  • @swaroopmaddu
    @swaroopmaddu Před 4 lety

    Great explanation I have seen Thanks

  • @anyagapyuk2423
    @anyagapyuk2423 Před 3 lety

    Good explanation. Thank you.

  • @Andrew-ut2wi
    @Andrew-ut2wi Před 4 lety

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

  • @ABHIRAMR1
    @ABHIRAMR1 Před 2 lety

    Amazing explanation 🤩🔥🔥🔥

  • @ashrafsiddiqui7147
    @ashrafsiddiqui7147 Před 3 lety

    you are a genius man.

  • @jjchoi08
    @jjchoi08 Před 3 lety +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 Před rokem

    Good Screening Explanation...Keep it up

  • @tathagatnegi5923
    @tathagatnegi5923 Před 4 lety +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🥺

  • @valentinmorales2633
    @valentinmorales2633 Před 8 měsíci

    Here is your like good man, excellent explanation!

  • @folklinoff
    @folklinoff Před 4 lety

    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 ❤

  • @aparna8338
    @aparna8338 Před 4 lety

    Would be helpful if u do more of these algorithm type videos

  • @deadelinor
    @deadelinor Před 3 lety

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

  • @madhukiran8409
    @madhukiran8409 Před 4 lety +1

    5:50 😂😂😂 nick white taught me

  • @adil6873
    @adil6873 Před 4 lety +4

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

  • @prateek4524
    @prateek4524 Před 3 lety

    I like your way of explaining. More power to you , lots os success awaits for your channel💪💪🙏🔥🔥🔥🤩🤩

  • @jamalsimpson6139
    @jamalsimpson6139 Před 3 lety

    5:43 that was totally a Michael Cera moment lol

  • @brandonquach2550
    @brandonquach2550 Před 3 lety

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

  • @uberwebd9824
    @uberwebd9824 Před 2 lety

    best explanation - ty

  • @SoftwareCares
    @SoftwareCares Před 2 lety

    Excellent!!

  • @ankitsharma3490
    @ankitsharma3490 Před 4 lety +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 Před 2 lety

      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

  • @charlesopuoro5295
    @charlesopuoro5295 Před 8 měsíci

    Thanks for this