Largest Sum Contiguous Subarray | D E Shaw & Co Interview Questions | GeeksforGeeks

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ก.พ. 2025

ความคิดเห็น • 114

  • @mihnea-adriantoma4506
    @mihnea-adriantoma4506 4 ปีที่แล้ว +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 2 ปีที่แล้ว +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].

  • @mohsinqureshi2671
    @mohsinqureshi2671 2 ปีที่แล้ว +3

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

  • @satadhi
    @satadhi 7 ปีที่แล้ว +37

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

  • @SmartProgramming
    @SmartProgramming 6 ปีที่แล้ว +5

    helpful video, thanks 👍👍🙂🙂

  • @sitimartoon2521
    @sitimartoon2521 2 ปีที่แล้ว

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 2 ปีที่แล้ว

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

  • @chiranjevkoul2376
    @chiranjevkoul2376 8 ปีที่แล้ว +4

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

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  8 ปีที่แล้ว

      Thanks, Chiranjev! :)

    • @achienbui9347
      @achienbui9347 3 ปีที่แล้ว

      @@GeeksforGeeksVideos hello bro, Can you turn on subtitles for your videos?

  • @farhadpagdiwala1789
    @farhadpagdiwala1789 4 ปีที่แล้ว

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

  • @justiceeziefule9308
    @justiceeziefule9308 2 ปีที่แล้ว

    Well explained. Thanks.

  • @aniketkhare8635
    @aniketkhare8635 4 ปีที่แล้ว +1

    noone can explain better than this. Thanks a lot !

  • @sitimartoon2521
    @sitimartoon2521 2 ปีที่แล้ว

    This Explanation is enough beginners....

  • @mritunjay7065
    @mritunjay7065 4 ปีที่แล้ว

    Super Explanation SIR !!!!

  • @sallamtanna742
    @sallamtanna742 2 ปีที่แล้ว

    best explanation

  • @zahidfayaz
    @zahidfayaz 7 ปีที่แล้ว +1

    Thank you, will try to implement it later on CPP

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 ปีที่แล้ว

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

  • @AmithAdiraju1994
    @AmithAdiraju1994 8 ปีที่แล้ว +3

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

  • @manojpandey7517
    @manojpandey7517 4 ปีที่แล้ว +22

    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 4 ปีที่แล้ว +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 4 ปีที่แล้ว

      exactly. anyone with basic skills can explain the pseudocode.

  • @samaryadav4530
    @samaryadav4530 7 ปีที่แล้ว +1

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

  • @komalvenkatesh4527
    @komalvenkatesh4527 5 ปีที่แล้ว +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.

  • @concretewindow1078
    @concretewindow1078 8 ปีที่แล้ว +2

    Very helpful for my Algorithms class. Thank you.

  • @BlokeBritish
    @BlokeBritish 2 ปีที่แล้ว

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

  • @shriya3265
    @shriya3265 4 ปีที่แล้ว

    very nice explanation, thankyou.

  • @smartshakku4u
    @smartshakku4u 5 ปีที่แล้ว +13

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

    • @Bhatonia_Jaat
      @Bhatonia_Jaat 4 ปีที่แล้ว

      how?

    • @sarveshkesharwani
      @sarveshkesharwani 4 ปีที่แล้ว +1

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

    • @SNM_23745
      @SNM_23745 4 ปีที่แล้ว +1

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

  • @jsiva4310
    @jsiva4310 2 ปีที่แล้ว

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

  • @kamaninikhil71
    @kamaninikhil71 6 ปีที่แล้ว +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

  • @rajeshwariadi5330
    @rajeshwariadi5330 5 ปีที่แล้ว +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 5 ปีที่แล้ว

      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.

  • @chanakyahosamani3618
    @chanakyahosamani3618 4 ปีที่แล้ว

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

  • @adelbuilds
    @adelbuilds 6 ปีที่แล้ว

    This was really helpful, thanks !!

  • @Arushtech4
    @Arushtech4 3 ปีที่แล้ว

    Thank you

  • @Amit11Singh11
    @Amit11Singh11 6 ปีที่แล้ว

    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 6 ปีที่แล้ว +2

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

    • @percydai8131
      @percydai8131 5 ปีที่แล้ว +1

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

  • @theanu7bhava
    @theanu7bhava 7 ปีที่แล้ว +29

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

    • @GTX4747
      @GTX4747 7 ปีที่แล้ว +6

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

    • @theanu7bhava
      @theanu7bhava 7 ปีที่แล้ว +1

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

    • @andre.queiroz
      @andre.queiroz 7 ปีที่แล้ว +35

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

    • @theanu7bhava
      @theanu7bhava 7 ปีที่แล้ว +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 7 ปีที่แล้ว +6

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

  • @nishanthsanjeevi5035
    @nishanthsanjeevi5035 7 ปีที่แล้ว

    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

  • @dilipprajapati2719
    @dilipprajapati2719 2 ปีที่แล้ว

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

  • @jiganeshpatil1472
    @jiganeshpatil1472 4 ปีที่แล้ว

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

  • @tarushidubey2973
    @tarushidubey2973 ปีที่แล้ว

    thank you sir

  • @kshitijgupta592
    @kshitijgupta592 3 ปีที่แล้ว

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

  • @ankitkaushik9229
    @ankitkaushik9229 6 ปีที่แล้ว

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

  • @ayushgupta2903
    @ayushgupta2903 4 ปีที่แล้ว +1

    How this is related to Dynamic programming?

    • @ashkrjha
      @ashkrjha 4 ปีที่แล้ว +1

      Because you are remembering what was the maximum so far!

  • @darkknigh874
    @darkknigh874 4 ปีที่แล้ว

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

    • @darkknigh874
      @darkknigh874 4 ปีที่แล้ว

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

  • @namanjain9922
    @namanjain9922 4 ปีที่แล้ว

    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.

  • @34_roshansingh_it80
    @34_roshansingh_it80 ปีที่แล้ว

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

  • @okansarca2657
    @okansarca2657 7 ปีที่แล้ว

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

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 ปีที่แล้ว +7

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

  • @anirudhpai2774
    @anirudhpai2774 4 ปีที่แล้ว

    thank you!

  • @nishuvats4436
    @nishuvats4436 7 ปีที่แล้ว +1

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

  • @divyashankar6427
    @divyashankar6427 4 ปีที่แล้ว

    thankyou so much

  • @Sakshi-nd2jz
    @Sakshi-nd2jz 4 ปีที่แล้ว

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

  • @weiqing2582
    @weiqing2582 5 ปีที่แล้ว +1

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

    • @hariyaligajera639
      @hariyaligajera639 5 ปีที่แล้ว

      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 4 ปีที่แล้ว

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

  • @adhitya946
    @adhitya946 7 ปีที่แล้ว

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

  • @harshadapol969
    @harshadapol969 3 ปีที่แล้ว

    This algorithm fails when array is [-1]

  • @SunilSingh1994
    @SunilSingh1994 8 ปีที่แล้ว

    Awesome !

  • @CricketVideosRG
    @CricketVideosRG 4 ปีที่แล้ว

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

  • @nsarunasrinivasan9904
    @nsarunasrinivasan9904 8 ปีที่แล้ว

    very nice

  • @sumishajmani705
    @sumishajmani705 7 ปีที่แล้ว

    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 7 ปีที่แล้ว

      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 6 ปีที่แล้ว

      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

  • @rishabhsahu5257
    @rishabhsahu5257 5 ปีที่แล้ว

    many will be overwhelming if you explain in hindi also

  • @adityasodhiya3987
    @adityasodhiya3987 4 ปีที่แล้ว

    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.

  • @hacksbsb
    @hacksbsb 5 ปีที่แล้ว

    3哥的口音好难懂。。

    • @HarshGangwar
      @HarshGangwar 4 ปีที่แล้ว

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

  • @sitimartoon2521
    @sitimartoon2521 2 ปีที่แล้ว

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 2 ปีที่แล้ว

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 2 ปีที่แล้ว

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 2 ปีที่แล้ว

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 2 ปีที่แล้ว

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 2 ปีที่แล้ว

    We want this type of explanation for all DSA problems

  • @sitimartoon2521
    @sitimartoon2521 2 ปีที่แล้ว

    We want this type of explanation for all DSA problems