Kadane's Algo in 16 minutes || Algorithms for Placements

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • In this Video, we are going to learn about Kadane's Algorithm.
    There is a lot to learn, Keep in mind “ Mnn bhot karega k chor yrr apne se nahi hoga ya maza nahi aara, Just ask 1 question “ Why I started ? “
    [For 20% Discount ] Visit Coding Ninjas: bit.ly/3cfdKTe
    Discord Server Link: / discord
    Course Flow: whimsical.com/...
    Slides Link: drive.google.c...
    Code Links: github.com/lov...
    Questions Links:
    - Kadane's Algo - leetcode.com/p...
    Do provide you feedback in the comments, we are going to make it best collectively.
    Connect with me here:
    Instagram: / lovebabbar1
    Twitter: / lovebabbar3
    Telegram Group Link: Love Babbar CODE HELP
    telegram.me/lo...
    My Editor: www.instagram....
    Intro Sequence: We have bought all the required Licenses of the Audio, Video & Animation used.
    #DP_Series #LoveBabbar

Komentáře • 225

  • @odiacomdeyu.s3148
    @odiacomdeyu.s3148 Před rokem +25

    When you study great teachers… you will learn much more from their caring and hard work than from their style. TQ sir

  • @cptshr
    @cptshr Před rokem +26

    This was an amazing example sir, if we wanted we would have made this code a lot more heavier and complex. Increasing its complexity.
    But your video showed us an follow process to make the code optimal and reduce the complexity to lowest possible.

  • @RajeshSharmaIndia
    @RajeshSharmaIndia Před rokem +5

    i just came to your video , i am afraid about DSA , Now i just see that video and now i am confident that i can also solve the problems, your way of teaching is quite simple and easy. thanks a lot.

  • @Caroline-up8qw
    @Caroline-up8qw Před rokem +6

    Nooooo one can explain better than him...Thank you Sir for such an easy explanation

  • @francksgenlecroyant
    @francksgenlecroyant Před 11 měsíci +3

    so nice to see someone who can explain things just as easy as this, I understood everything concept he explained while I can speak nor hear a single word in indian language, imagine if he were speaking in English 🤣🤣, a great teacher always finds a way to make students understands what he means. Thanks India for producing such indian talents.

  • @khemendrabhardwaj2552
    @khemendrabhardwaj2552 Před rokem +6

    Even if we are asked for size of longest subarray which yields the max sum , we can use Concept of Variable size window to find the sum(mx sum using kadane ) . Time => O(n)

    • @Asad-pq7el
      @Asad-pq7el Před rokem

      bhai 2 loop use karke all subarray kaise nikal sakte hai?

    • @discarded1669
      @discarded1669 Před rokem

      @@Asad-pq7el take a vector sum and intial array of size n, then
      go i = 0->n, where there's a var x = 0 & max = 0
      then run j = i -> n, where x += arr[j] and if arr[j] beginner than max update it and also store the i & j index in another var a,b..
      after the whole iteration is completed, you'll have a, b which is the starting & finishing index of the longest subarray with max sum..

  • @maheshbk-dh1co
    @maheshbk-dh1co Před rokem +3

    You are going to revolutnize the coding universe....💯💯
    love from nepal..🥰

  • @Cas7nova
    @Cas7nova Před rokem +6

    The playlist that I've been searching all over the internet and finally you started it.., thankyou for making it❣️

  • @riteshko123
    @riteshko123 Před 27 dny

    Some Nigaa Said "This was an amazing example sir, if we wanted we would have made this code a lot more heavier and complex. Increasing its complexity. But your video showed us an follow process to make the code optimal and reduce the complexity to lowest possible."

  • @user-up6sl2gq8p
    @user-up6sl2gq8p Před rokem +1

    One of the most important approach for the complex problem of array.

  • @kashyapit
    @kashyapit Před rokem +3

    The also doesn't work for negative numbers, You need different algorithm. Following is the working implementation in Javascript:
    function maxSubArray(nums) {
    let maxSoFar = nums[0];
    let maxEndingHere = nums[0];
    for (let i = 1; i < nums.length; i++) {
    maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
    maxSoFar = Math.max(maxSoFar, maxEndingHere);
    }
    return maxSoFar;
    }

  • @amarpreetkaur6056
    @amarpreetkaur6056 Před rokem +17

    Bhaiya,
    A suggestion for Dsa series--
    Could you please create a schedule kinda like when to start when to end what ,
    An excel sheet or something where we can enter our start date nd the rest dates will b decided logically for whole dsa plan ,this will give us a fair idea of which vdo/homework/tasks should take what time for completion.
    We can stick to that and try to complete the course within the deadlines as mentioned in the sheet.
    Problem in self is that we don't get an idea how much to do each day or how to allocate number of days to each topic,thus we exaggerate the things without proper plan leading to non completion of tasks

  • @appajik858
    @appajik858 Před 2 měsíci

    explained from basics, so best explanation ever thank uuuu😇

  • @ChutHunter
    @ChutHunter Před rokem

    Simplest and BEST explaination ever! Thank you so much babbar bhaiya.

  • @iteself_me
    @iteself_me Před rokem

    170 Bhaiya aapke ecplainations ki etni aadat hui hai ki ab baki khi se bhi padho kuch samj nahi aata
    Besttesstttt explaination & Bhaiyaaaa Everrrr😍

  • @uzerkhan1758
    @uzerkhan1758 Před rokem +2

    Bhaiyya ,love the video and solution,
    Please upload the lecture on-
    "sliding windows algorithm"

  • @Anjali-bw9hd
    @Anjali-bw9hd Před 7 měsíci +1

    doesnt work if all numbers in array are -ve, maxi will return a negative integer rather than returning 0.

  • @vivekvatsalya8836
    @vivekvatsalya8836 Před rokem

    Thank you bhaiya ....just isko sikhne ke bad maine lagatar 2 questions solve kar di with some modifications for different problems

  • @softwareengineering101

    I have gone through multiple videos none of them helped me to understand Kandane's algorithm. This video does

  • @anirudhjain7598
    @anirudhjain7598 Před rokem +14

    KMP Algorithm next pls??🙃

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

    thankyou bhaiya for easy and to the point concept of kadane's algo.

  • @dikshantmalusareh1458

    Another game changing playlist started...kudos to you sir...DAY 1 ✅

  • @surajmendhe2594
    @surajmendhe2594 Před rokem +1

    Thank you so much sir nice explanation and easy to understand ❤️

  • @ayusheditz8300
    @ayusheditz8300 Před rokem +2

    Efforts >>>3 man ❤️

  • @prashusahu6610
    @prashusahu6610 Před rokem +1

    Thank You Bhaiya for Completing the Remaining Algorithms

  • @hehejoker2059
    @hehejoker2059 Před 9 měsíci

    Nice Man just opened my concepts crystal clear

  • @its_sagar371
    @its_sagar371 Před 6 dny

    Best Explanation bhaiya

  • @kritikaagarwal810
    @kritikaagarwal810 Před 4 měsíci

    Simply great explanation, great bhaiya 🔥🔥🙌🙌

  • @AbhishekGupta-vu7xd
    @AbhishekGupta-vu7xd Před rokem

    Ek baar mein hi samjh aa gaya thanks mahan guru ji 😊

  • @siddharthsinghaniya3177

    if all the element in the array is -ve then apply this
    long long maxSubarraySum(int arr[], int n){

    long long max = arr[0];
    long long sum=0;
    for(int i=0;imax)
    max = sum;
    if(sum

  • @vinaykumaryadav8718
    @vinaykumaryadav8718 Před rokem

    Wonderful explanation.... did't know that. it is that much easy.

  • @Vishal_84_k
    @Vishal_84_k Před rokem +1

    bhoat time se wait kr Raha tha ..

  • @sirkartik
    @sirkartik Před rokem +3

    Thankyou bhaiya, I understood it clearly now...😄 I wanted to ask that will us be ever able to actually make these kind of algorithms ourseleves? Like learning sliding windows, kadane's algo and other algos is different thing and making them is different.

    • @rohanverma3111
      @rohanverma3111 Před rokem +2

      To actually make algorithms you can do your research/phd in algorithms. These algorithms were made in budding days of computer science and algos are mainly build by mathematicians. Like this kadane algo was made in 1980s

    • @sirkartik
      @sirkartik Před rokem +2

      @@rohanverma3111 Thanks for the info brother👍

    • @sirkartik
      @sirkartik Před rokem +2

      @@rohanverma3111 I want to ask that do developers make algorithms or they just see it from somewhere, or learn it and only implement it?

    • @rohanverma3111
      @rohanverma3111 Před rokem +2

      @@sirkartik I hardly believe that any developer is building some amazing algorithms in their day to day job. No dev will write a new algo. Even if you make a decent algorithm in a project codebase on your own but it won't be approved by your seniors if some standard algo is out there. If you are interested about making algorithms you can opt for MS/PHD in CSE and can take your thesis as Algorithms. To discover new things you need to pursue research.

  • @truptigaikwad7515
    @truptigaikwad7515 Před 12 dny

    Thanks I was stuck at this program from last 3 days,sab ho gya tha sum

  • @prateekjain262
    @prateekjain262 Před 3 měsíci

    third step needs more explanation why we need to make sum 0 if sum becomes -ve anuj bhaiya have explained it well if some one does not understand please checkout his video once.

  • @abknand579
    @abknand579 Před 9 měsíci +1

    You found the sum but which sub array produced that highest sum was not shown in code. How do we highlight which sub array has the highest sum?

  • @mdyusufansari8897
    @mdyusufansari8897 Před 3 měsíci

    is this correct ? python
    arr = [-1,2, 2, -3,4, -4, 5]
    max = 0
    current = 0
    for i in arr:
    current += i
    if current < 0 :
    current = 0
    if current > max :
    max = current
    print(max)
    print(current)

  • @neerkhandor5007
    @neerkhandor5007 Před rokem +8

    Bhaiya if possible can you start series on solving your 450 Dsa cracker sheet
    If you provide a bit overview of the problem also then it will be very much helpful as your explanation is excellent
    Thank you bhaiya 🙏

  • @piyushs1511
    @piyushs1511 Před rokem

    Bhaiya swad aa jaata haii apki video dekh k❤💯

  • @revolverlim.6444
    @revolverlim.6444 Před 5 měsíci +1

    mst samjaya bhaiya

  • @kashyapit
    @kashyapit Před rokem +1

    Doesn't work on all negative numbers. I have to search correct Kadane's also.

  • @prathamjoshi3969
    @prathamjoshi3969 Před rokem

    truly the best explaination

  • @AKSHAYKUMAR-ht1tl
    @AKSHAYKUMAR-ht1tl Před 2 měsíci

    majaa aa gyaa bro. keep it up!!

  • @avibirla9863
    @avibirla9863 Před rokem +1

    Hello Jiii ...web development course kabhi upload honge bhaiya ?Excited to learn web development from ur end...

  • @user-xl5xx8co7v
    @user-xl5xx8co7v Před 9 měsíci

    I understood the approach, thanks bhaiya but I am not understanding why this one is not working, I understand that this is not the efficient approach but yet, what I am doing wrong
    class Solution(object):
    def maxSubArray(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    n=len(nums)
    self.maxSum=-float('inf')
    if n==0:
    return 0
    if n==1:
    return nums[0]
    def helper(nums,i,curr_sum):
    if i>n-1:
    self.maxSum=max(self.maxSum,curr_sum)
    return curr_sum

    op1=helper(nums,i+1,curr_sum+nums[i])
    op2=helper(nums,i+1,nums[i])
    self.maxSum=max(op1,op2)
    return max(op1,op2)
    helper(nums,0,0)
    return self.maxSum

  • @deepakchaurasia6952
    @deepakchaurasia6952 Před 3 měsíci

    why are you taking sum =0 if sum

  • @BornHubisLive
    @BornHubisLive Před rokem +1

    Thankuu bro... For your efforts..❤🙏

  • @ekanshsharma1309
    @ekanshsharma1309 Před rokem

    Thank you so much bhaiya😁❤️
    So much love to you

  • @ashoksolanki7895
    @ashoksolanki7895 Před rokem

    most valuble content bhaiya majaa aa gya
    😀😀

  • @thirdcreater4231
    @thirdcreater4231 Před rokem +1

    Bhaiya plz make videos on rabin karp algo and kmp algo

  • @rahulchavda9063
    @rahulchavda9063 Před rokem

    understood the concept thank you babbar bhai

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

    Amazing explanantion.

  • @pradeepkumarsharma52
    @pradeepkumarsharma52 Před rokem +1

    Kudos to your hardwork 👏

  • @yashendrabadal4776
    @yashendrabadal4776 Před rokem

    thanks bhaiya you made dsa easy for me

  • @vedantraut256
    @vedantraut256 Před rokem +1

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

  • @dheerajsharma355
    @dheerajsharma355 Před rokem

    good explanation.....How all are here after todays daily challenges at leetcode lol?

  • @kumarpawansahh
    @kumarpawansahh Před rokem

    Ye bari achi initiative liya hai aapne bhaiya 👍👍

  • @rishikeshsarangi1245
    @rishikeshsarangi1245 Před rokem

    great explanation! thank you for this

  • @tejaswijadhav8031
    @tejaswijadhav8031 Před rokem

    Amazing Explanation Sir

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

    Algo samjha diya apne code khud karunga😂

  • @BongoLasss
    @BongoLasss Před rokem +2

    Sir ,
    What if all the elements were negative....so in that case, how could we implement the condition for less than 0

    • @Griffith_1802
      @Griffith_1802 Před rokem

      did you get the answer ?

    • @johnysins69696
      @johnysins69696 Před rokem

      @@Griffith_1802 some has mentioned this code:
      function maxSubArray(nums) {
      let maxSoFar = nums[0];
      let maxEndingHere = nums[0];

      for (let i = 1; i < nums.length; i++) {
      maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
      maxSoFar = Math.max(maxSoFar, maxEndingHere);
      }

      return maxSoFar;
      }

  • @cruzer_blade.
    @cruzer_blade. Před 7 měsíci +1

    thank you sir

  • @soumadipmajila7962
    @soumadipmajila7962 Před rokem

    Any upcoming live web development batches? Your CZcams content has been super helpful, but I want to try a live class for a more interactive learning experience. Let me know if you have any plans or updates. Thanks!

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

    Can anyone tell me ans for maximum subarray sum for
    [2,0,7,8] using the above algorithm
    please

  • @royz-byabhinavraj7459

    But in the question it is asked to give the sub array too where is the sub array ?

  • @rudreshkarn2155
    @rudreshkarn2155 Před rokem +2

    @CodeHelp Although all the test cases passed. I don't think this code should pass the case where all the Elements are negative.

    • @Smitiraiv
      @Smitiraiv Před rokem +2

      It would because the sum

  • @TON-108
    @TON-108 Před rokem

    You made it easy thank you 😌

  • @GopalSingh-zn2fb
    @GopalSingh-zn2fb Před 6 měsíci +14

    for the all the negative number this approach will fail

    • @Cristianoronaldo0702
      @Cristianoronaldo0702 Před 5 měsíci +3

      It will work
      Because each time sum become 0 as it will become negative for each iteration
      Then for the greatest negative no(smallest in magnitude) sum will become equal to that no. Ans maxi will become that no. By max (sum, maxi) and that no will come output as maxi will not get updated further

    • @Nova-vc3bh
      @Nova-vc3bh Před 3 měsíci

      Will work

    • @moohsinatabassum5915
      @moohsinatabassum5915 Před 3 měsíci

      Bro Just sort and print arr[0] if all negative

    • @labhanshugupta
      @labhanshugupta Před 3 měsíci

      @@moohsinatabassum5915complexity will increase if we do that

    • @enzaki7603
      @enzaki7603 Před měsícem +1

      It will work bro 😅 just update the maximum before you update the sum to 0. That's simple : )

  • @knowledgedoctor3849
    @knowledgedoctor3849 Před rokem

    Sliding Window Playlist is Missing, Better Add Some Video Regarding Problem Solving About Sliding Window... Unacademy Live Session Me Maja Nehi Aiya😔 Pls Babbar Bhai Last Wala Topic Cover Kar Dho..
    Love From Bangladesh

    • @depelterturbo
      @depelterturbo Před rokem

      Try coming to this side by illegal border crossing...

  • @simran_bhargava
    @simran_bhargava Před 2 měsíci

    anyone explain the second approach in o(n^2) time complexity

  • @anishkhari4900
    @anishkhari4900 Před 9 měsíci

    If all the elements are negative then the max sum of contiguous subarray is not zero.

  • @ManishYadav-dt2qc
    @ManishYadav-dt2qc Před rokem

    Achha lga app ka btane ka trika ♥️

  • @yashjain5412
    @yashjain5412 Před rokem

    if we consider all +ve integer in array then max sum should be 12

  • @twaritagrawal1232
    @twaritagrawal1232 Před rokem +1

    thankyou sir

  • @sarabjeetsingh297
    @sarabjeetsingh297 Před 7 měsíci

    sir mazza aa gya 😊

  • @ahsanahmad8600
    @ahsanahmad8600 Před rokem

    Nice explanation sir ......

  • @ranitnaha9892
    @ranitnaha9892 Před rokem

    If the array contains all negative integers than what will be the sum? since here u are excluding all the negative sum,,

  • @sukhjitsingh959
    @sukhjitsingh959 Před rokem

    Thanks for new vedio Babbar

  • @programmingwithvishnu2601

    Great Explanation

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

    thank you sir 👌👌👌👌

  • @ankuranand5844
    @ankuranand5844 Před rokem

    Sir,According to me you cannot get all the subarrays from only 2 loops which you have used ! if so please anyone clear my doubt!

  • @stat_life
    @stat_life Před rokem

    What if we dont need maximum sum but instead want those subpairs which make maximum sum???

  • @ramveer1558
    @ramveer1558 Před rokem

    can not find symbol
    maxi =max(maxi , sum);
    symbol: method max(int , int)
    bro i am face this error please resolve this

  • @ABHI-ez9hp
    @ABHI-ez9hp Před rokem

    Kudos to your effort :)

  • @taison0072
    @taison0072 Před rokem

    New series algorithms hurray 🙂

  • @namratakaushik22
    @namratakaushik22 Před rokem

    nice explanation

  • @sahilanand30
    @sahilanand30 Před rokem +1

    Best explanation
    Segtree wen?

  • @mohammedinam4324
    @mohammedinam4324 Před rokem

    Best Explanation

  • @pallavi9362
    @pallavi9362 Před rokem

    amazing explanation!!!!!!!!!

  • @d3vanshmemer
    @d3vanshmemer Před rokem

    very easily described

  • @hafizsakib2734
    @hafizsakib2734 Před rokem

    9:37 agar sum = -1 aya,then max(-1,-2) = -1; to hum kiu sum ko ignore kare jaab sum 0 se kam aye??

  • @abhijeetbasfore6816
    @abhijeetbasfore6816 Před rokem +1

    String algorithm , segment tree krwa do

  • @modanwalpriti8769
    @modanwalpriti8769 Před rokem

    Thankyou for this video bhiaya

  • @lostcyrus8578
    @lostcyrus8578 Před rokem

    good explanation bhaiya

  • @pankajlade7894
    @pankajlade7894 Před rokem +1

    Bhaiya, can you share 2nd approach with time Complexity O(N2) .I just want to cross Verify my code ...

  • @anupamojha9343
    @anupamojha9343 Před rokem

    Bhaiya please two pointer approach and sliding window algo ko bhi cover kr do

  • @user-ll4ze9bz3r
    @user-ll4ze9bz3r Před 4 měsíci +1

    Sir ye to fail ho jayega na jb saare hi elements negative ho to, kyu sum =0 zero rakhoge ??

    • @shivampandey2662
      @shivampandey2662 Před 2 měsíci

      It will work for all negative elements too, do dry run once

  • @MakeuPerfect
    @MakeuPerfect Před rokem +1

    Bhaiya sliding window ka ak video lao

  • @pankajpoonia6043
    @pankajpoonia6043 Před rokem

    sir can you please make a video on fermat little theorem and wilson theorem 🙏🙏🙏🙏🙏

  • @vanshaj2018
    @vanshaj2018 Před rokem

    10133 /10134 test cases passed...the code worked 2 months ago now its updated
    what's the one exception?

    • @yashjain5651
      @yashjain5651 Před rokem

      Use long long as dtypes for sum and max_element variable.