Largest Sum Contiguous Subarray | GeeksforGeeks

Sdílet
Vložit
  • čas přidán 31. 10. 2016
  • Find complete code at GeeksforGeeks article: www.geeksforgeeks.org/largest-...
    Practice Problem Online Judge: practice.geeksforgeeks.org/pro...
    This video is contributed by Harshit Jain.

Komentáře • 114

  • @satadhi
    @satadhi Před 7 lety +35

    dont you think you are missing out a lot of corner cases ??

  • @mihnea-adriantoma4506
    @mihnea-adriantoma4506 Před 4 lety +7

    An optimization for the case where all numbers are negative,it would be to check at the end of for loop if the max_so_far is 0 and only if it is 0 you make another for loop and find the maximum negative element.
    Since in real scenarios it does not happen too often to have all numbers negative,the above approach is better

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

      You don't have to loop again. You can just initialize the variables to the first element of the array. i.e max_so_far = max_ending_here = A[0].

  • @sitimartoon2521
    @sitimartoon2521 Před rokem

    We want this type of explanation for all DSA problems

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

    Explained simply well. great work dude. You are helping many to understand coding better.

  • @SmartProgramming
    @SmartProgramming Před 5 lety +5

    helpful video, thanks 👍👍🙂🙂

  • @adhitya946
    @adhitya946 Před 6 lety

    Hey Admin do you have java tutorials for the beginners? If so please send me the link address

  • @farhadpagdiwala1789
    @farhadpagdiwala1789 Před 3 lety

    Very helpful and clearly explained with the concept of Dynamic Programming.

  • @rajeshwariadi5330
    @rajeshwariadi5330 Před 5 lety +1

    If I want to keep track of the starting and ending positions of the maximum sum subarray using the dp solution, how should I go about with it?

    • @prabhatism
      @prabhatism Před 5 lety

      every time you reset the value to 0 for max_ending_here keep that index + 1 value saved, assume, start_index.
      every time you update max_so_far save with it [start_index, current value of iterator]
      then you splice it and print it.

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

    noone can explain better than this. Thanks a lot !

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

    Great tutorial! You are doing great service to the community.

  • @chanakyahosamani3618
    @chanakyahosamani3618 Před 3 lety

    Is there an efficient solution to find the start and end index of the maximum contiguous subaaray?

  • @sitimartoon2521
    @sitimartoon2521 Před rokem

    This Explanation is enough beginners....

  • @adelbuilds
    @adelbuilds Před 5 lety

    This was really helpful, thanks !!

  • @samaryadav4530
    @samaryadav4530 Před 7 lety +1

    thank you. Please post the video using Divide and Conquer for the same.

  • @sitimartoon2521
    @sitimartoon2521 Před rokem

    We want this type of explanation for all DSA problems....

  • @zahidfayaz
    @zahidfayaz Před 7 lety +1

    Thank you, will try to implement it later on CPP

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  Před 7 lety

      Great!
      Here's a practice problem for this: practice.geeksforgeeks.org/problems/kadanes-algorithm/0

  • @AmithAdiraju1994
    @AmithAdiraju1994 Před 7 lety +3

    Thank you so much @GeeksforGeeks, I couldn't find any better explanation than this !!

  • @justiceeziefule9308
    @justiceeziefule9308 Před rokem

    Well explained. Thanks.

  • @BlokeBritish
    @BlokeBritish Před 2 lety

    is it possible to find the least distance between all characters of a longest common subsequence ?

  • @jsiva4310
    @jsiva4310 Před rokem

    Can we have one code for the positive, positive & negative and all negative numbers?

  • @kamaninikhil71
    @kamaninikhil71 Před 5 lety +1

    Looks like this algo is returning sum of max possible sub array , but not actually the subarray, As per the title and the question algo should return the contiguous subarray

  • @shriya3265
    @shriya3265 Před 4 lety

    very nice explanation, thankyou.

  • @mritunjay7065
    @mritunjay7065 Před 3 lety

    Super Explanation SIR !!!!

  • @concretewindow1078
    @concretewindow1078 Před 7 lety +2

    Very helpful for my Algorithms class. Thank you.

  • @nishuvats4436
    @nishuvats4436 Před 7 lety +1

    the example using dynamic programming is not just for contiguous subarray but all possible subarrays right?

  • @sallamtanna742
    @sallamtanna742 Před 2 lety

    best explanation

  • @anirudhpai2774
    @anirudhpai2774 Před 3 lety

    thank you!

  • @komalvenkatesh4527
    @komalvenkatesh4527 Před 5 lety +1

    We could easily make this work for both positive and negative arrays by modifying max_ending_here = max (max_ending_here + a[i], a[i]) and remove the 'max_ending_here = 0' assignment.

  • @Arushtech4
    @Arushtech4 Před 3 lety

    Thank you

  • @okansarca2657
    @okansarca2657 Před 6 lety

    the algorithms fails when the array is {-2, -3, 4, -1, -12, 1, -5, 3} it finds 1 as max sum

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  Před 6 lety +7

      It seems to be producing 4 as output.
      Please see: ide.geeksforgeeks.org/yjoLoCMS5R

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

    How this is related to Dynamic programming?

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

      Because you are remembering what was the maximum so far!

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

    It will fail for arrays having all negative values. e.g. [-1,-2,-3].

    • @Bhatonia_Jaat
      @Bhatonia_Jaat Před 3 lety

      how?

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

      still the max value will be -1 so it is correct

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

      public int maxSubArray(final List A)
      {
      int end =0,far=0,counter=0;
      for(int i=0;i

  • @dilipprajapati2719
    @dilipprajapati2719 Před rokem

    This code dosen't work if occurance of negative number is greater than positive number.

  • @tarushidubey2973
    @tarushidubey2973 Před rokem

    thank you sir

  • @divyashankar6427
    @divyashankar6427 Před 4 lety

    thankyou so much

  • @namanjain9922
    @namanjain9922 Před 3 lety

    this video explain about working of algorithm with an example which is very good but can anybody tell me how to reach to this algorithm by yourself if no such algorithm exists like I want to know how to think of this solution.

  • @jiganeshpatil1472
    @jiganeshpatil1472 Před 4 lety

    BEST Tutorial can you please explain the problem, Angry children, 2 of hacker rank, please it is challenging

  • @SunilSingh1994
    @SunilSingh1994 Před 7 lety

    Awesome !

  • @theanu7bhava
    @theanu7bhava Před 7 lety +30

    what if all elements are negative, then this would fail!

    • @GTX4747
      @GTX4747 Před 7 lety +6

      thats the idea, who the hell you want to find the largest sum in negative array

    • @theanu7bhava
      @theanu7bhava Před 7 lety +1

      Tal K I want to find. and this could be a use case in gaming etc..

    • @andre.queiroz
      @andre.queiroz Před 7 lety +35

      this works only if not all are negative. If all are negative, you just pick the greatest number in the sequence.

    • @theanu7bhava
      @theanu7bhava Před 7 lety +2

      so, first i will traverse in array to know if all are negative, and then i will againcheck for greates element. 2n solution you are saying?

    • @andre.queiroz
      @andre.queiroz Před 7 lety +6

      Anubhava Gupta yes. First check if all are negative, if yes, return the greatest. If not, do the algorithm

  • @34_roshansingh_it80
    @34_roshansingh_it80 Před 7 měsíci

    if array value is only -1 then you get error in this code

  • @harshadapol969
    @harshadapol969 Před 3 lety

    This algorithm fails when array is [-1]

  • @kshitijgupta592
    @kshitijgupta592 Před 3 lety

    It's not working if all the elements of the array are negative

  • @Amit11Singh11
    @Amit11Singh11 Před 5 lety

    For the input [1,2,-3,4,5,-6,7,8] it's returning 18, whereas the answer should be 7+8 = 15

    • @AlekhMaheshwari
      @AlekhMaheshwari Před 5 lety +2

      18 is correct. The subarray would be 4+5+(-6)+7+8 =18

    • @percydai8131
      @percydai8131 Před 5 lety +1

      [4,5,-6,7,8] = 18 > [7,8] = 15

  • @ankitkaushik9229
    @ankitkaushik9229 Před 5 lety

    You can omit using variable "max_so_far" as you can return "curr_max"

  • @darkknigh874
    @darkknigh874 Před 4 lety

    Don't you think this algorithm is missing the scenario where all numbers will be negative.

    • @darkknigh874
      @darkknigh874 Před 4 lety

      Sorry didn't see the second part that will solve the all negative case

  • @Sakshi-nd2jz
    @Sakshi-nd2jz Před 3 lety

    You are literally only reading out what is written over there in the website

  • @nishanthsanjeevi5035
    @nishanthsanjeevi5035 Před 6 lety

    if all the elements are negative then just return the maximum value of the array
    so use a flag to check if the input array has at least one positive element

  • @sumishajmani705
    @sumishajmani705 Před 6 lety

    can anybody tell me how will we print largest contiguous sum array that is in this case 4,-1,-2,1,5 .!! #GeeksforGeeks

    • @cherubim7
      @cherubim7 Před 6 lety

      Hi Sumish, have a look at my post. It also finds out the starting and ending indices of Maximal subarray using the Kadane's algorithm: www.thecodenote.com/2017/11/guide-to-solving-maximal-subarray.html

    • @deehanchowdhury1230
      @deehanchowdhury1230 Před 6 lety

      max_so_far =4, max_ending_here=4,max_ending_here=3,max_ending_here=1,max_ending_here=2,max_ending_here=7, max_so_far=7...done!! you take all the elements in the array and get the maxsum. +sumish ajmani

  • @nsarunasrinivasan9904
    @nsarunasrinivasan9904 Před 7 lety

    very nice

  • @weiqing2582
    @weiqing2582 Před 5 lety +1

    if the array is [-1] or [-1, -2]
    it will return 0;
    it should return -1 . , -2 respectively

    • @hariyaligajera639
      @hariyaligajera639 Před 5 lety

      int Solution::maxSubArray(const vector &A)
      {
      int max_so_far=INT_MIN,max_ending_here=0;
      int size=A.size();
      for(int i=0;imax_so_far)
      max_so_far=max_ending_here;
      if(max_ending_here

    • @DemGains
      @DemGains Před 4 lety

      add "while max(arr) < 0; return max(arr)"

  • @rishabhsahu5257
    @rishabhsahu5257 Před 5 lety

    many will be overwhelming if you explain in hindi also

  • @CricketVideosRG
    @CricketVideosRG Před 4 lety

    If it is the input is only -1 it won’t work

  • @manojpandey7517
    @manojpandey7517 Před 4 lety +21

    I don't find this kind of explanation useful where you straightaway start proving your pseudocode right instead of explaining the intuition behind it. mycodeschool is better in terms of it.

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

      dude we need it to find the max subsequence sum.. he is explaining how to do it. you are here because you want to understand how do code works.. not why we need it since why we need it is for what ever rzn you are here.

    • @VishalPatel_imvishal
      @VishalPatel_imvishal Před 3 lety

      exactly. anyone with basic skills can explain the pseudocode.

  • @hacksbsb
    @hacksbsb Před 5 lety

    3哥的口音好难懂。。

    • @HarshGangwar
      @HarshGangwar Před 4 lety

      @We Can www.google.com/search?q=chinese+to+english&oq=chines+to+&aqs=chrome.1.69i57j0l4j69i60l3.2219j0j9&sourceid=chrome&ie=UTF-8

  • @adityasodhiya3987
    @adityasodhiya3987 Před 3 lety

    You NEED to explain the intuition/approach behind the problem NOT THE PSEUDO CODE. Anyone can understand code, but not the approach. This is just lazy work with no creativity.

  • @sitimartoon2521
    @sitimartoon2521 Před rokem

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 Před rokem

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 Před rokem

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 Před rokem

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 Před rokem

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 Před rokem

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 Před rokem

    We want this type of explanation for all DSA problems