Kadane's Algorithm | Largest Sum Contiguous Subarray | Java and C++ | Anuj Bhaiya ✅DSAOne Course #13

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 23. 02. 2021
  • Hey guys, In this video, we'll be solving Largest Sum Contiguous Subarray Problem using Kadane's Algorithm.
    Practice here: practice.geeksforgeeks.org/
    🚀 Follow me on:
    Instagram: / anuj.kumar.sharma
    Linkedin: / sharma-kumar-anuj
    Twitter: / realanujbhaiya
    ╔═╩╗╔╩╗╔═╩═╩╩╩╩╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    đŸ„ł Join our Telegram Community:
    Telegram channel: t.me/coding_enthusiasts
    Telegram group: t.me/dsa_one
    💾 Visit www.educative.io/anuj to avail discount on all courses on Educative!
    💾 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!
    📚 Complete DSA Playlist: ‱ DSA-One Course - The C...
    Complete Android Development Playlist: ‱ Android Development Tu...
    Hashtags:
    #anujbhaiya #dsaone #kadane #algorithm
    Ignore these tags:
    kadane's algorithm
    kadanes algorithm
    kadane
    kadane algorithm
    algorithm
    maximum sum subarray kadane algorithm
    kadane's
    algorithms
    kadanes algorithm maximum sum subarray
    kadanes algorithm to maximum sum subarray
    kadane's algoritm
    kadane's algorithm maximum sum subarray
    kadane algorithm java
    kadanes
    kadanes algorithm java
    data structure algorithms kadanes algorithm
    kadane's algorithm in c
    kadane's algorithm java
    kadanes algorithm explained
    kadane's algorithm
    kadane algorithm
    kadanes algorithm
    algorithm
    kadane
    kadane algorithm c++
    kadane's
    kadane algorithm java
    algorithms
    kadanes algorithm maximum sum subarray
    kadanes algorithm to maximum sum subarray
    kadane's algorithm maximum sum subarray
    kadane's algorithm in c
    maximum sum subarray kadane algorithm
    kadane algorithm 2d
    kadane algorithm dp
    kadane algorithm complexity
    kadane's algorithm to maximum sum subarray problem
    kadane's algorithm
    kadane algorithm
    kadane's algo v.v.v.v.v imp
    largest sum contiguous subarray
    kadanes algorithm
    maximum subarray
    maximum subarray problem
    maximum sum subarray
    maximum subarray sum
    anuj bhaiya
    maximum subarray leetcode
    kadane's algo
    kadens algorithm
    kadane’s algorithm
    subarray
    kadane
    kadane algo
    kadanes algo
    maximum sub array
    maximum product subarray
    max subarray
    kadane's algorithm in hindi
    max subarray sum
    find largest sum contiguous subarray
    algorithm
    maximum circular subarray sum
    max sum subarray
    kandane algorithm
    leetcode 53
    max sum contiguous subarray
    subarray with given sum
    max sub array
    maximum subarray
    dsa
    kadane algorithm in hindi
    split array largest sum
    max product subarray
    longest and subarray
    subarray sum
    dsa one
    max circular subarray sum
    maximum frequent subarray sum
    continuous subarray sum
    subarray sum equals k
    algorithms in java
    anuj bhaiya dsa
    array algorithms
    leetcode maximum subarray
    maximum sum triplet
    algorithms
    greedy algorithm
    anuj
    array algorithm
    find subsequence of length k with the largest sum
    kadane's algorithm hindi
    maximum sum rectangle
    aditya verma dynamic programming
    contiguous subarray
    kadane's algorithm jenny
    maximum beauty subarray
    maximum sub array sum
    maximum sum circular subarray
    subset sum problem
    two pointer
    algorithm course
    k-th largest sum contiguous subarray
    longest subarray
    max subarray problem
    maximum frequent subarray sum codechef solution
    maximum subarray sum cases
    anuj bhaiya java
    apna college
    contiguous array
    dsa course
    find largest sum contiguous subarray [v. imp]
    given an array arr[] of n integers. find the contiguous sub-array(containing at least one number) which has the maximum sum and return its sum.
    kadens algo
    longest and subarray codechef
    maximize the sum hackerearth
    maximum contiguous sub-array problem
    partition array for maximum sum
    algorithm in java
    kadane's algorithm
    kadane algorithm
    kadane's algo v.v.v.v.v imp
    largest sum contiguous subarray
    maximum subarray
    kadanes algorithm
    maximum subarray problem
    maximum sum subarray
    maximum subarray leetcode
    maximum subarray sum
    anuj bhaiya
    kadane's algo
    kadens algorithm
    kadane’s algorithm
    subarray
    kadane algo
    kadane
    kadanes algo
    maximum sub array
    max subarray
    maximum product subarray
    kadane's algorithm in hindi
    max subarray sum
    find largest sum contiguous subarray
    algorithm
    maximum circular subarray sum
    53. maximum subarray
    leetcode 53
    max sum subarray
    kandane algorithm
    max sum contiguous subarray
    subarray with given sum
    max sub array
    dsa
    split array largest sum
    kadane algorithm in hindi
    subarray sum
    max product subarray
    longest and subarray
    dsa one
    leetcode maximum subarray
    max circular subarray sum
    maximum frequent subarray sum
    continuous subarray sum
    algorithms in java
    greedy algorithm
    subarray sum equals k
    anuj bhaiya dsa
    array algorithms

Komentáƙe • 841

  • @Krishna-le2vz
    @Krishna-le2vz Pƙed 3 lety +22

    Bhaiya aap hmare liye itni mehnat krte h to apkr liye like to bnta h
    Love u bhaiya ♄♄

  • @vishnuvardhanreddy9075
    @vishnuvardhanreddy9075 Pƙed 3 lety +7

    Watched many videos but now finally I understood this algorithm clearly. Excellent explanation bro. Keep doing the work.

  • @masqueradeee
    @masqueradeee Pƙed 3 lety +50

    I was struggling for hours with this algorithm, Thanks for the detailed explanation.

  • @anshulkumar1550
    @anshulkumar1550 Pƙed 3 lety +9

    Bhai sahab !!!! Kitne acche se samjhaya aapne. Maza aa gaya Anuj Bhaiya. Thank you.

  • @ritiagrawal4051
    @ritiagrawal4051 Pƙed 2 lety +48

    This is once of the best explanation i have heard about any DS algorithm. You not only explained the solution but explained how should we think. thanks a lot

    • @mdyusufansari8897
      @mdyusufansari8897 Pƙed 2 měsĂ­ci

      can you help me pleawse

    • @SumitK-n8p
      @SumitK-n8p Pƙed měsĂ­cem

      ​@@mdyusufansari8897 ladki se hi q help chiye

  • @jaishreesaraswat3629
    @jaishreesaraswat3629 Pƙed 3 lety +323

    I knew this algorithm but was struggling with an explanation ....really appreciable work !

    • @world4376
      @world4376 Pƙed 2 lety +5

      i too did you get placed ????

    • @devyanshbansal8728
      @devyanshbansal8728 Pƙed 2 lety +4

      Damnn right i was not able to understand the tutorial in my paid course

    • @pranav288
      @pranav288 Pƙed 2 lety

      @@devyanshbansal8728 F

    • @rbk.technology4747
      @rbk.technology4747 Pƙed 2 lety +9

      I guess the tweek is to make maxsum as -INF and not 0.
      My point is if all elements are neg - then simply the task is now to return the max in neg arr.
      Make this before hand.

  • @shubhammazumdar8436
    @shubhammazumdar8436 Pƙed 2 lety +3

    Best explanation so far !! Have watched many videos to get clarity on this but your video turned out to be the best. !!

  • @sabbiralshafi8684
    @sabbiralshafi8684 Pƙed 3 lety +3

    my watching lots of videos, I have to say that it is the best one. after watching for 2 minutes I understand the whole concept.

  • @aksheshdoshi
    @aksheshdoshi Pƙed 3 lety +2

    Very good explanation. I looked at other videos with way more views than this but this is the only way I understood it (that too in an intuitive way).

  • @TaherAli-wb4ob
    @TaherAli-wb4ob Pƙed rokem

    This is the easiest explanation on kadane's Algorithm by far that i have seen on YT. Thank you for explaining the though process.

  • @rohitk472
    @rohitk472 Pƙed rokem +14

    for all negative values ---->
    class Solution {
    public:
    int maxSubArray(vector& nums) {
    int current = 0;
    int max = -10000000000;
    for(int i = 0; i < nums.size(); i++){
    current = current + nums[i];
    if(current > max){
    max = current;
    }
    if(current < 0){
    current = 0;
    }
    }
    return max;
    }
    };

    • @srinivassr5067
      @srinivassr5067 Pƙed měsĂ­cem

      dude u have literally written the same one that he explained.
      You were trying to solve for one with all elements being -ve right?

  • @monotonic_non_decreasing4618

    Bhaiya, sahi mai maja aa gaya, poora samaj aa gaya, ek baar mai, khud algo banayi maine aapke samjaane k baad, thodi mistake kardi thi, aapka code dekha phir correct kar li, thank you bhaiya, I wish you good health and great life ahead..

  • @karanrai6772
    @karanrai6772 Pƙed 3 lety +3

    Bhaiya sach me pure youTube pr ap or hi ho jo achhe se explain krte ho love ❀ you dil se bhaiya

  • @mohammadbilal1316
    @mohammadbilal1316 Pƙed 3 lety +2

    Thank you anuj bhaiya aapne jo samjhaya kadane algo ko itne easiest way me wo koi aur nhi samjha paaya aap hamare guru ho programming ke again thank you 3000❀

  • @siddharthtripathy3964
    @siddharthtripathy3964 Pƙed 2 lety +1

    finally found a video which actually explains rationale behind each line of an algo.

  • @sameerk047
    @sameerk047 Pƙed 2 lety +1

    Superb explanation, I was breaking my head to understand the logic to get max sub array when all the element are negative.

  • @aniketborkar9185
    @aniketborkar9185 Pƙed 2 lety +9

    Nice explanation!
    For an array with negative values:
    int max(int a, int b)
    {
    int temp = a>b?a:b;
    return temp;
    }
    int maxSubArray(const int* A, int n1) {
    int i,j,curr_sum=A[0],max_sum=A[0];
    for(i=1;i

    • @hemant64616
      @hemant64616 Pƙed 4 měsĂ­ci

      if the question is this a = [1] .. why it is not working and showing error give me its solution it showing greater value 0 the actual answer is 1

  • @ahmadfarazansari284
    @ahmadfarazansari284 Pƙed 2 lety +3

    Best explanation of kadane's algorithm so far.

  • @202_b_ashishojha8
    @202_b_ashishojha8 Pƙed 2 lety +27

    If all the elements are even negative :
    int current_sum = 0, max_sum =INT_MIN;
    for(int i =0;i

    • @Jarvis-cw5vu
      @Jarvis-cw5vu Pƙed 2 lety +2

      To the point solution bro!!

    • @rajeevsankhla5028
      @rajeevsankhla5028 Pƙed 2 lety +2

      @@Jarvis-cw5vu max_sum=nums[0]; also works fine

    • @sparshtaneja
      @sparshtaneja Pƙed 2 lety +1

      @@rajeevsankhla5028 only if array is sorted

    • @kunalsoni3906
      @kunalsoni3906 Pƙed rokem +6

      if all the elements are negative then it should print the lowest negative integer and this code is not working.

    • @vishalsinghpanwar2972
      @vishalsinghpanwar2972 Pƙed rokem +1

      @@rajeevsankhla5028 how did you get the intuition to use max_sum=nums[0] instead of max_sum = 0 like told in the video?

  • @Rakshit_Jain
    @Rakshit_Jain Pƙed rokem +2

    I tried this algo from apna college also , no compe to this elaboration đŸ€đŸ»

  • @rohitrohra7553
    @rohitrohra7553 Pƙed 2 lety +1

    Anuj bhaiya , u earned a subscriber , for explaining this. Thanks for the explanation and thought process. You ar doing a wonderful job

  • @vivekkr83
    @vivekkr83 Pƙed rokem +2

    By hearing the name I was thinking it would some complex algo. But you explained it very well, its very simple. Thank you

  • @nancyagarwal8570
    @nancyagarwal8570 Pƙed 3 lety +29

    Saw ur video day before yesterday, and yesterday I gave one written exam and same question comes, and I did in 2 mins.
    Thankyou for such wonderful explaination. :)

  • @mohdsahil226
    @mohdsahil226 Pƙed 2 lety +3

    zabardast explaination. Didn't get this kind of explaination even on udemy! Heads off! Thank you!

  • @sahilarora7186
    @sahilarora7186 Pƙed 3 lety +1

    Bhaiya. Abhi urvi didi ka explanation dekha . Smjh ni aya.. fir search kra kaden's algo. Apka search ke top pe aya. 1 bar m smjh aa gya. Ye. 🙏Thanks a lot.

  • @anjonroy7408
    @anjonroy7408 Pƙed 3 lety +1

    So nicely explained . Appreciate your effort. Thanks a lot

  • @raisanjeeb42
    @raisanjeeb42 Pƙed 3 lety +2

    Thank You bhaiya...I was struggling a lot in this algorithm..But This one video and all concepts clear

  • @mohitnegi3641
    @mohitnegi3641 Pƙed 3 lety +2

    Thanks bro. made it simple and something I will never forget

  • @sagarjoshi3481
    @sagarjoshi3481 Pƙed 2 lety

    Thanks,
    For the great explanation. I am struggling hours in this problem and got a very big solution, but your answer is short and easy to understand.

  • @ayushthakur733
    @ayushthakur733 Pƙed 3 lety +1

    Keep making such videos, your explanation is so brotherly islye acha smjh aata h

  • @crazeeealgorithms3236
    @crazeeealgorithms3236 Pƙed 3 lety +1

    Great Explanation , A big Thank You..., post more such videos.

  • @aneezahayatshaikh3176
    @aneezahayatshaikh3176 Pƙed 3 měsĂ­ci +1

    Really very nicely explained and also in very easy manner to understand... thanku so much sir!!!

  • @pawanchandrajoshi841
    @pawanchandrajoshi841 Pƙed 2 lety +59

    this one works for negatives as well :)
    int maxSubArray(vector& nums) {
    int sum=nums[0], max_sum=nums[0];
    for(int i=1; imax_sum)
    {
    max_sum=sum;
    }
    }
    return max_sum;
    }

  • @rudro314
    @rudro314 Pƙed 3 lety +35

    Best dsa course ever on youtube

    • @mohdzunaid9309
      @mohdzunaid9309 Pƙed 3 lety +1

      @@sumanmalakarvlogs18 I can definitely say that!

    • @rudro314
      @rudro314 Pƙed 3 lety +1

      @@sumanmalakarvlogs18 of course....apna college isn’t good at all

    • @ray-lw7bf
      @ray-lw7bf Pƙed 3 lety +2

      @@sumanmalakarvlogs18 1)apna college C++
      Course aacha present kiya hai..bt utna depth me analysis nhi kiya code ko
      2) dsa one me anuj bhaiya ne bhut systematic video banai hai...

  • @shivangigupta8196
    @shivangigupta8196 Pƙed 3 lety +1

    i saw many videos but your explanation was great thanks alot

  • @058__shivamverma7
    @058__shivamverma7 Pƙed 4 měsĂ­ci +1

    It can easily handle negative elelments also
    class Solution{
    long maxSubarraySum(int arr[], int n)
    {
    long sum=0;
    long maxi=arr[0];
    for(int i=0;imaxi){
    maxi=sum;
    }
    if(sum

  • @AllDimension
    @AllDimension Pƙed 2 lety

    Bhaiya maza agaya itna ache se mei kisise bhi nhi padha ye algorithm , ek baar mei pura dimag k andar.

  • @abhishekarora437
    @abhishekarora437 Pƙed 3 lety +2

    Very good course... Watching it since start.

  • @anuraggulati2167
    @anuraggulati2167 Pƙed 2 lety +1

    cleared all the doubts thankyou anuj bhaiya..

  • @balakrishnanr648
    @balakrishnanr648 Pƙed 2 lety

    Bhaiya, do more courses like this, as in-depth thought process is what most needed, then comes next thing that is - link multiple probs which applies almost same technique with variation.

  • @hamidakbar42
    @hamidakbar42 Pƙed rokem

    Thanks bruh..... I don't use to comment on videos. But this explanation forced me to do so. Bhai bahut hi simple samjhaya hai bhaiya ne. If anyone is struggling with this lect is for you

  • @gitanjalikumari9262
    @gitanjalikumari9262 Pƙed rokem +1

    GREAT EXPLANATION..THANKS BHAIYA

  • @Piyush-yq9iu
    @Piyush-yq9iu Pƙed 2 lety +1

    Thank You for such an amazing lecture.

  • @deveshsrivastava9121
    @deveshsrivastava9121 Pƙed 2 lety

    Thank you Anuj Bhaiya (SIR)
    Keep up the good work.

  • @yogeshdharya3857
    @yogeshdharya3857 Pƙed 3 lety

    Thanx Dada.Theory se samajh nahi pa raha tha.Video explanation se clear hua .Love and support from Jabalpur

  • @fenil861
    @fenil861 Pƙed 3 lety +1

    Simple and clear explanation. Thank you

  • @govindam94
    @govindam94 Pƙed 2 lety

    Explanation is very good and easy to undestand. If every element is negative then it is very simple, just check which element is least negative.

  • @Subash.IIT-Delhi
    @Subash.IIT-Delhi Pƙed 3 lety +80

    If all elements are negative then just traverse the array and find the element which is having samallest magnitude that will be the answer

    • @259_parthpatidar9
      @259_parthpatidar9 Pƙed 3 lety +3

      yes bro it is simple like that

    • @venkateshvenky2880
      @venkateshvenky2880 Pƙed 3 lety +16

      Initialize maxsum as minus infinity...

    • @rahulsaw8838
      @rahulsaw8838 Pƙed 3 lety +8

      Sum be will be equal to zero if all element are -ve and if sum equal to zero then maintain a variable for greatest element and scan while taking input

    • @KrishnaGupta-xd6xu
      @KrishnaGupta-xd6xu Pƙed 3 lety +1

      @@rahulsaw8838 So lengthy solution.

    • @ravikishankumar332
      @ravikishankumar332 Pƙed 3 lety +3

      @@venkateshvenky2880 yes right bro. this will also work for all negative element.

  • @AyushSingh-qr3go
    @AyushSingh-qr3go Pƙed 3 lety +2

    Thanks a lot bhaiya after struggling a lot I got right video and channel 🙏

  • @Onesided.x
    @Onesided.x Pƙed 6 měsĂ­ci +1

    Great explanation sir i didn't even saw a simple explanation like you. Although great 👍 sir

  • @uk1523
    @uk1523 Pƙed 3 lety +41

    Initially maxSum with INT_MIN

    • @mr.crazyunknown7094
      @mr.crazyunknown7094 Pƙed 3 lety

      not really....blank array is a valid subarray.... so In case the max sum is negative ...you can just take a blank subarray....and thus max_sum will be non-negative (i.e. zero or positive)
      Besides...starting with INT_MIN...and you face a negative number....you will run into overflow

    • @ayushthakur733
      @ayushthakur733 Pƙed 3 lety +3

      @@mr.crazyunknown7094 what if all the elements are negetive

    • @mr.crazyunknown7094
      @mr.crazyunknown7094 Pƙed 3 lety +2

      @@ayushthakur733 the proper output in that case should be an empty subarray.....so the sum will be zero..
      There are variations in the question tho....sum question might ask you to find non-empty subarray

    • @adityasinghrathore3935
      @adityasinghrathore3935 Pƙed 3 lety

      @@mr.crazyunknown7094 It depends. Leetcode expects subarray of length > 0

    • @himanshusharma9239
      @himanshusharma9239 Pƙed 3 lety

      if all elemnts are negative then it will work

  • @vibhutipatel2315
    @vibhutipatel2315 Pƙed 2 lety +5

    Thank you so much bhaiya, you explained it very smoothly and nicely. Here we just need to make " maxSum = Integer.MIN_VALUE " (in java) for the correct output for all negative elements.!!

  • @abhinavsaxena9846
    @abhinavsaxena9846 Pƙed 3 lety +2

    Thank you bhaiya you explained better than the teachers of apna cllgđŸ„°đŸ„°đŸ„°đŸ„°đŸ„°

  • @rohit45749
    @rohit45749 Pƙed 9 měsĂ­ci +1

    After , seeing code the overall concept is clear at the end , thank you bhaiya

  • @swapnilpatel9831
    @swapnilpatel9831 Pƙed 2 lety +1

    Best explanation in whole CZcams.

  • @subodhkumarbr
    @subodhkumarbr Pƙed 3 lety +9

    Thanx a lot Bhaiji From Inner of Sole for providing this course
    It might look crazy but I am a JEE Aspirant but still watch your DSA Course in my free time because i am very much enthusiast about the way of your teaching and know there student every crux of matter
    Again to say THANK YOU VERY MUCH

  • @kritartha007
    @kritartha007 Pƙed 3 lety +2

    Works for every cases:
    int maxSubarraySum(int arr[], int n){

    int currMax=0;
    int Max=0;
    for(int i=0;i

    • @kritartha007
      @kritartha007 Pƙed 3 lety

      Logic: If after adding arr[i] to current sum i.e.{ currSum+=arr[i]}, if current Sum is less than arr[i] or the added number, then this means left subarray sum is smaller than arr[i]. So , we update currSum=arr[i].
      else, if current sum is not less than arr[i], then there is no issue.
      Now, we have to check current sum with the maximum sum.
      If current sum> maximum sum-----> update maximum sum to current sum.
      after all the numbers are traversed , simply return the maximum sum

  • @Anonymous-coder31
    @Anonymous-coder31 Pƙed 3 lety +2

    Amazing easy to understand
    Thanks bhaiya 😍😍😍

  • @gauravanand_11
    @gauravanand_11 Pƙed 3 lety +3

    You cleared my concept :)

  • @manvibhardwaj725
    @manvibhardwaj725 Pƙed 2 lety

    Thank you sir, the explanation is fantastic.

  • @chandrabhachatterjee2472
    @chandrabhachatterjee2472 Pƙed 2 lety

    you explained this like crystal clear concept

  • @vasudhatapriya6315
    @vasudhatapriya6315 Pƙed 3 lety +1

    Greatly Explained!

  • @mrcharm767
    @mrcharm767 Pƙed 2 lety +1

    Bhaiyaa apki approach hamei badiya lagii please lage rehna aise lectures k sath hamara support apke sath h

  • @somnathchakraborty1965
    @somnathchakraborty1965 Pƙed 3 lety

    Thank you bhaiya for explaining very simple way....

  • @hrishikeshmhetre9163
    @hrishikeshmhetre9163 Pƙed 3 lety +2

    is vale number ka "yogdan" yahi tak tha . satya vachan bhaiya...

  • @extremesatyaiitjeeair1980

    incase the array is all of negative elements and you want subarray of atleast 1 size then you have to add this code after Kadane's algo
    if(maxsum==0)
    {
    maxsum=a[0];
    for(int i=1;imaxsum) maxsum=a[i];
    }

  • @_HarshitSharma
    @_HarshitSharma Pƙed 2 lety +1

    Jio bhaijaan ! Boht bdhiya explanation..!

  • @AmanSingh-rz1rj
    @AmanSingh-rz1rj Pƙed 3 lety +3

    Wow bhaiya really helpfull carry on bhaiya 😊

  • @SAKSHIKUMARIP
    @SAKSHIKUMARIP Pƙed 2 lety +1

    Clear explanation .tqqq sooo much bhaiya

  • @Nandhubhai_bolthe.
    @Nandhubhai_bolthe. Pƙed rokem

    i very glad because i understand only in single vedio thankyou so much for this vedio

  • @n-az563
    @n-az563 Pƙed rokem +3

    I struggled for this algorithm a lot even for O(N2) I could not get it.Thank you for your explanation

  • @tushargarg3765
    @tushargarg3765 Pƙed 3 lety +57

    Thank you for the nice explanation but,
    Maxsum should be initialized to A[0] otherwise in the case of A=[-2] it will give 0 as output.

  • @surajbaranwal56.
    @surajbaranwal56. Pƙed 2 lety +1

    God bless you bhaiya, very well explained.

  • @technocratsujal
    @technocratsujal Pƙed rokem

    Kya behetreen Samjhaya hai Bhaiya Aapne Love You!

  • @Yash_Parashar
    @Yash_Parashar Pƙed 3 lety +2

    Best dsa course bhaiya inspiration 🙏🙏🙏

  • @amankeshri3823
    @amankeshri3823 Pƙed 3 lety +11

    Initialize MaxSum with very large -ve values (within range of integer) then it will pass all test cases

    • @swagatpandak7325
      @swagatpandak7325 Pƙed 2 lety

      but will occupy more space than when you initialize it with max_sum=nums[0]; it will pass all cases and also occupy less space.

  • @kanikamatta3503
    @kanikamatta3503 Pƙed 2 lety +7

    If we initialize max = arr[0] then this will work for negative cases also

  • @rohitrohra7553
    @rohitrohra7553 Pƙed 2 lety +1

    Once i get a switch i ll give the highest thanks , as a token a appreciation.

  • @varunyadav6560
    @varunyadav6560 Pƙed 3 lety +1

    Great EXPLANATION 👏

  • @rajattiwari6697
    @rajattiwari6697 Pƙed rokem +4

    Nice Explanation.I found one error .maxSum should be initialized by a[0] as for example if the first element is -1,then some test cases will be failed .And for all negative it will be replaced by INT_MIN

  • @jyotipandey3156
    @jyotipandey3156 Pƙed 3 lety +27

    U r the best... No animation content can ever beat uâ€ïžâ€ïžâ€ïžâ€ïžđŸ˜˜....loads of luv....it's quite help full ..a saashtaang dandwat pranaam to ur laymen type of explanation...plz complete this series as soon as possible â€ïžâ€ïžđŸ‘đŸ‘đŸ‘đŸ‘đŸ™

    • @rajeshagarwal4137
      @rajeshagarwal4137 Pƙed 3 lety +11

      Direct attack on apna college; tauntbazi

    • @harshittripathi3985
      @harshittripathi3985 Pƙed 3 lety +4

      @@rajeshagarwal4137 I came here to comment on apna college nautanki, nd suddenly I found ur comment 😂😂

  • @faizanusmani2200
    @faizanusmani2200 Pƙed 3 lety

    Very easily explained. Zabardast Video.

  • @krishanuchakraborty3500
    @krishanuchakraborty3500 Pƙed 2 lety +7

    If all the elements are negative or otherwise, we can declare the maxSum like below:
    int maxSum = Integer.MIN_VALUE;

  • @bhavyagupta3565
    @bhavyagupta3565 Pƙed rokem

    op i love you, bada pyara samjhaya!!!

  • @himanshuchoudhary2514
    @himanshuchoudhary2514 Pƙed rokem +1

    BEST VIDEO ON KADANE'S ALGO .... I SEEN MANY VIDEOS BUT THE WAY BHAIYA IS TEACHING IS SUPER AMAZING.....CLEARING THE WHOLE CONCEPT ....THANK YOU...............I SEEN IN MANY VIDEOS THEY JUST MEMORISE THE CONCEPT BUT IN THIS VIDEO I KNOW WHAT THE ALGO MEANS...AND THATS THE KEY ....🧠😊

    • @nagpuri4390
      @nagpuri4390 Pƙed rokem

      Why we have taken sum=0 when sum

    • @himanshuchoudhary2514
      @himanshuchoudhary2514 Pƙed rokem

      @@nagpuri4390 it because negative value will decrease the total sum of subarray..

  • @user-ds4ym3hs4e
    @user-ds4ym3hs4e Pƙed 3 měsĂ­ci +1

    Your explanation was everything 🙌 ❀

  • @brijeshkumarverma1223
    @brijeshkumarverma1223 Pƙed 2 lety +1

    correct ans for gfg que is:
    long long maxSubarraySum(int arr[], int n){
    // Your code here
    long long cur_sum=0;
    long long max_sum=INT_MIN;
    for(int i=0;imax_sum){
    max_sum= cur_sum;
    }
    if(cur_sum

  • @ritikkhandelwal2358
    @ritikkhandelwal2358 Pƙed 3 lety

    Anuj bhaiya.....bhot sahi bhot sahi😍😍

  • @Lucifer-jl9vh
    @Lucifer-jl9vh Pƙed 2 lety +1

    Good explanation..Thank you Anna

  • @animeshkumar_corporatevlogger

    maxSUm should be initialised with INT_MIN, great explanation bhaiya. Really helpfull.

  • @TheMdaliazhar
    @TheMdaliazhar Pƙed 2 měsĂ­ci

    This is great and easiest explanation i have found. Thanks for sharing this for free. your explanations are in general better than many other popular youtubers in same category.
    Suggestion: This code will fail in case `all the numbers are negative`. and to handle this edge case we just need to initialized top two variables with undefined value and use ternary operator during value assignment.

  • @ebrahimali2689
    @ebrahimali2689 Pƙed 2 lety

    your explanation is just wow.

  • @jayeshwaghmare2989
    @jayeshwaghmare2989 Pƙed 3 lety +169

    if all elements are negative then max sum should be initialized to minus infinity or INT_MIN

    • @dhruvpatel2039
      @dhruvpatel2039 Pƙed 2 lety +26

      Better keep min_sum equal to the first element

    • @shivamsinha5554
      @shivamsinha5554 Pƙed 2 lety +5

      bro both are same int min is the lowest value that can be stored in interger variable

    • @joyboy1088
      @joyboy1088 Pƙed 2 lety +20

      @@shivamsinha5554 [-3, -1] take this array and try to find out the max sum using the approach taught. (ans is -1 but you will 0 since we are ignoring the negative numbers)

    • @kmohammedalthaf3535
      @kmohammedalthaf3535 Pƙed 2 lety +1

      yes exactly, it's a solution for any number...

    • @pranay_gupta
      @pranay_gupta Pƙed 2 lety +3

      @@joyboy1088 simpy initialize with arr[0]

  • @hardikverma5191
    @hardikverma5191 Pƙed 2 lety

    thanks a lot bhaiya!!! very nicely explained : - )

  • @ritiksaxena1984
    @ritiksaxena1984 Pƙed rokem +1

    This will work for all the negative as well as positive nums
    int ms,cs;
    ms=cs=arr[0];
    for(int i=1;i

  • @Knowledgeduniya1432
    @Knowledgeduniya1432 Pƙed 2 lety

    Beauifully explained bhaiya thanku

  • @ABHISHEKKUMAR-bl5wy
    @ABHISHEKKUMAR-bl5wy Pƙed rokem

    Great explaination sir !!!!!!!!

  • @anshumanupadhyaycodes
    @anshumanupadhyaycodes Pƙed 2 lety +1

    liked a video on YT after a long time!

  • @MayankSharma-sf3hy
    @MayankSharma-sf3hy Pƙed 2 lety

    So good would love to see more

  • @himanshuthakur272
    @himanshuthakur272 Pƙed 2 lety

    Thanks bhaiya...you explained nicely..