Facebook Coding Interview Question - findLongestSubarrayBySum

Поделиться
HTML-код
  • Опубликовано: 1 окт 2024

Комментарии • 258

  • @RogerGarrett
    @RogerGarrett 4 года назад +274

    I worked my entire career (45 years) as a software engineer and I never, ever had to address a coding problem like this. Why in the world are these kinds of questions part of programmer interview questions?

    • @tofahub
      @tofahub 4 года назад +22

      It teaches us newcomers how to think

    • @berylliosis5250
      @berylliosis5250 4 года назад +33

      It makes sure you know how to develop an algorithm and how to make sure you don't wreck your big O

    • @brandon5058
      @brandon5058 4 года назад +25

      Roger Garrett Hi Mr. Garett, I was 13 years old and started programming. I am 20 now and without a degree have got a job as full stack dev, whereas I feel like these problems can help me be a better thinker, they aswell make me aware of these kind of problems like time complexity and the bruteforce problem and the sliding window approach. Thats why I think these questions contribute to a programmer interview.

    • @monkeygolfer
      @monkeygolfer 4 года назад +103

      It's because software engineer jobs pay 6 figures out of college so everyone started trying it. Now they need a way to weed out people.

    • @ifthatthenthis3797
      @ifthatthenthis3797 4 года назад

      I agree with you on this particular question but a lot of the questions that i see are pretty relevant this is shit

  • @thesanmi
    @thesanmi 3 года назад +10

    Sliding window with two pointer solution breaks down if you have negative numbers in the array.
    A more resilient approach is to add the sum as we go and put in a hashmap from sum to index of sum.
    So at index i, all we need to do is check if a complement sum exists i.e. (sumSoFar - Target) exists in the map and update if the value from the map map map[ i - index] is larger than max length. this is O(N), uses one pointer and works with negative entries

  • @popmadalin14
    @popmadalin14 4 года назад +32

    I love this kind of videos.Keep up the good work man

  • @Mahorir
    @Mahorir 4 года назад +36

    Can be optimised further by keeping the window size to at least the size of last found window

    • @elijahbuscho7715
      @elijahbuscho7715 4 года назад +1

      Not really. You add an additional operation EVERY iteration to remove a few operations on SOME iterations. There can be cases where it is an optimization, and other cases where it makes the program slower. Also both solutions are on the same order so it doesn't really matter.

    • @Portiadude
      @Portiadude 4 года назад +1

      @@elijahbuscho7715 But you will have FEWER iterations.
      The question here really is this: How often do we have to update the left pointer, and how often the right pointer? Well, the right pointer is N times, as we have to get to end of the array.
      The left pointer is what changes between the two proposals. Let L be the length of the largest sub array we want. If we update the left each we update the right, then we update the left about N - L times, as we stop updating the left once the right hits the end of the array.
      In the one where we have the left play catchup, we have to do at least N - L updates, as we could risk having to move the left pointer beoynd the N - L position.
      In short, we save some time, which could be quite a lot if the largest array is big and we have a few high values at the end.

  • @emmanuelonwumah915
    @emmanuelonwumah915 4 года назад +141

    I see a few people saying the nested while loop makes the time complexity n^2. It does not, because the inner loop is not depending on the size of n. This algorithm is still linear.

    • @NickWhite
      @NickWhite  4 года назад +25

      s/o to you my guy

    • @emmanuelonwumah915
      @emmanuelonwumah915 4 года назад +31

      @@NickWhite Yo I just started doing these coding challenges like a week ago and I've literally been cramming your channel because I have an interview with one of the FAANG companies next week. The only thing your channel does not go over in depth is recursion and dynamic programming. More videos on that would be awesome. 👏

    • @paragagrawal6668
      @paragagrawal6668 4 года назад +5

      @Emmanuel Onwumah I did not understand this. Say instead of 3 zeros, the array has 50 zeros, then won't the inner while loop will loop for 50 times, thereby not being linear? I get it won't loop for the length of the loop (being n) like the first loop but still the inner while loop in itself will be linear time and being inside another loop, it will make the time complexity of also n^2.

    • @emmanuelonwumah915
      @emmanuelonwumah915 4 года назад +4

      Parag Agrawal Ok so time complexity is only measured in reference to operation being performed * size of variable. In this case length of the array is the variable. The out while loop ONLY focus on the right pointer and the length. The inner while ONLY focuses on the left point and the sum. The inner loop is only invoked if that special case is met. It is not invoked on every iteration. Meaning if the rightpointer reaches arr.length the both loops will end regardless what is happening to the left pointer.

    • @paragagrawal6668
      @paragagrawal6668 4 года назад +1

      @@emmanuelonwumah915 Ohh! Got it. So its kinda inverse looping (from the norm of a brute force algo) where the outer loop is moving the right pointer and the inner loop is moving the left pointer. Got it. Thanks for explaining.

  • @studywithme3660
    @studywithme3660 4 года назад +19

    This is exactly how I thought I would solve it but didn't know it has a name "slicing window approach".

    • @dhananjaygupta4680
      @dhananjaygupta4680 3 года назад +6

      Sliding*

    • @InterviewDose
      @InterviewDose 3 года назад +1

      I heard these words and techniques only after coming to USA ...

    • @dantevale0
      @dantevale0 3 года назад

      You're a natural programmer then

  • @saulgoodman980
    @saulgoodman980 4 года назад +32

    One important thing to know is that this approach works as long as there are no negatives. (2 pointers usually don't work when there's negatives)

    • @09TYPER
      @09TYPER 2 года назад

      And sorted.

    • @Will-en6gw
      @Will-en6gw 2 года назад

      @@09TYPER I thought the same but then I re read the question. It says look for a contiguous sub array, so that means you cant alter the order of the array given to you. So there is no problem with the sliding window on this problem, sorted or not. Hope this makes sense.

    • @09TYPER
      @09TYPER 2 года назад

      @@Will-en6gw but if it is not sorted, whats the point of two pointer and do left++ and right-- ?

  • @mohamedsamir952
    @mohamedsamir952 4 года назад +17

    Impressive as usual it all about how you describe the answer and the problem I hope many creators learn from you

  • @Monir1993
    @Monir1993 4 года назад +7

    Nick I had an interview on super short notice and all I did was watch your videos. You were CLUTCH! Aced them. Thanks a lot for the content :)

  • @shahafzm
    @shahafzm 3 года назад +1

    this is better
    int[] result = new int[], {-1,-1};
    int left =0;
    int sum = arr[left];
    int right = 0;
    while(right < arr.length)
    {
    sum +=arr[right];
    if(sum == s)
    {
    result = new int[] {left,right};
    right++
    }
    else if(sum>s)
    {
    if(right-left>result[1]-result[0])
    {
    sum-=arr[left++];
    }
    else
    {
    right++;
    }
    }
    return result;

  • @total_dk6517
    @total_dk6517 4 года назад +6

    I might be mistaken, but instead of only incrementing your left pointer when current_sum is higher than s (while r != [-1]), could you not increment both pointers, because you don't care about any array that's shorter than your currently found subarray? (Of course, if you have not found a proper subarray (and r == [-1]), then you need to only increment the left pointer.)
    Of course, this would not reduce the solution to less than O(n), but would change it to worst case ~n instead of ~2n (if I'm not mistaken)

    • @jagaya3662
      @jagaya3662 4 года назад

      You can't increase both pointers running into problems (using 1-based index here):
      Imagine looking for sum=13 in [13, 1, 1, 1, 12]
      First hit is [1, 1]
      Next it will go through the loop until currentSum > sum at [2, 5] with a currentSum of 15.
      At this point increasing both pointers would both cause an error while also failing to produce the actual solution of [4, 5]

  • @hodex5763
    @hodex5763 3 года назад +2

    I guess the array must be sorted for this approach to work

  • @your-Space
    @your-Space 2 года назад +1

    Most optimal soln : Kadane's algorithm and it will work in O(n) time for even negative elements.where sliding window fails !!

  • @tzookbarnoy
    @tzookbarnoy 4 года назад +2

    Wondering how to solve this if I have negative numbers in the array :/

  • @dipper0yawn
    @dipper0yawn 3 года назад +1

    It took me more than an hour but I actually came up with the O(n) solution by myself.

  • @na_you_mess_am
    @na_you_mess_am 4 года назад +5

    Can you please read the problem description in your next videos, as it helps simulated a real life coding interview

    • @total_dk6517
      @total_dk6517 4 года назад +1

      At a real life coding interview, you can always ask the interviewer for clarification of the question if needed. Imagine this just simulating the clarification of the question.

  • @roootnova4837
    @roootnova4837 4 года назад +8

    Can we solve this problem by : 1. Sort array in ascending order
    2. Add up the sum for each array element
    I think it also require O(n).
    So, can we solve the problem by this approach?

    • @NickWhite
      @NickWhite  4 года назад +7

      Sorting is NlogN which is slower than O(N)

    • @roootnova4837
      @roootnova4837 4 года назад +2

      @@NickWhite Get it. Thanks

    • @treyquattro
      @treyquattro 4 года назад +8

      that wouldn't work. How do you determine the indices after the fact? What if there are repeated values - which one's which? Is your sort stable? But mostly you're completely changing the order of the values in the original array so you lose cohesion.

    • @porco-espinhoentediado6258
      @porco-espinhoentediado6258 4 года назад

      If you want another solution you could:
      1.Create an array pref[i]=arr[i]+arr[i-1]+...arr[0] O(n)
      2. So now if we want the sum of the segment arr[i...j] we can do pref[j]-pref[i]+arr[i]
      3. We can try some binary search stuff for every i=0...n-1 which would take O(nlogn)
      So yeah his two pointers solution is much better.

    • @anoopm3605
      @anoopm3605 4 года назад +1

      Sorting would not work because it will mess up the order. We need the subarray, not any combination to reach the sum

  • @lifinker874
    @lifinker874 3 года назад +1

    After solving it brillantly, the follow up question is: what if some numbers are negative?

    • @n0ame1u1
      @n0ame1u1 3 года назад

      Can we beat n^2 in that case?

  • @scoperesolution2915
    @scoperesolution2915 4 года назад +2

    I love thos channel. We want videos like this... All your videos are great @Nick.

  • @surajtopal9940
    @surajtopal9940 4 года назад +4

    Love from India ❤️ thanks. Brother for making these types of videos .

  • @Cloud-577
    @Cloud-577 2 года назад +1

    I hope interview questions were as easy as this :(

  • @antianti4331
    @antianti4331 2 года назад +1

    It's too easy for Facebook, isn't it?

  • @treyquattro
    @treyquattro 4 года назад +2

    so Nick, there's one thing I'm not quite getting - you say that you hope people can nail problems like this and get a job. You're a Leetcoding monster yet - I believe - you haven't got an offer yet? Is it because even rehearsing all the Leetcode, Facebook, Google, Amazon, blah blah questions in the world won't help in a live tech interview, or is there something else (the hemp debacle at Google notwithstanding)? It seems to me that on the basis of answering coding questions you should definitely be hired. It's a bit worrying if that's not the case. What does it take?!?!? Does this also mean that all these coding sites that are popping up from various youtubers are selling snakeoil if it makes no difference, regardless of the reams of testimonials they supposedly have (I'm very doubtful: at least one of those guys is a known BS artist). Thanks and good luck!

    • @NickWhite
      @NickWhite  4 года назад +1

      I’ve gotten plenty of offers

    • @NickWhite
      @NickWhite  4 года назад +1

      I currently have a job and also have had plenty of positions in the past please watch my video from save a lot to $55 / hour or you can see my LinkedIn in description

    • @treyquattro
      @treyquattro 4 года назад +1

      @@NickWhite OK, sorry: I mustn't have watched enough vids yet. I obviously came to the conclusion that you have been interviewing but no-one has seen fit to extend an offer. You're obviously a talented engineer so it'd be worrying if even someone with such a track record of solving problems is not getting offers. I hope you continue with the channel. Much respect for you and your many talents (not least the songs :D)

  • @danielmilyutin9914
    @danielmilyutin9914 4 года назад +1

    Are negative numbers allowed in input array? I missed it, but I guess not allowed. Because I can make counterexample input like [a0, ..., aN, -sumOfThose + desiredSumOfSubarray]

  • @Vijay-fi8bm
    @Vijay-fi8bm Год назад +1

    this doesn't work with unsorted arrays

  • @ninehoursful
    @ninehoursful 4 года назад +1

    @Nick White Thanks again for great explanation. Can you please answer just one more question
    I tried solving this question on my own before looking at this video on CodeSignal, I could solve it with single loop & sliding window however it couldn't cover the edge cases like *(new int[]{1, 0, 2, 0, 0, 0, 0, 0}, 0);*. I spent 2 hours figuring out a solution that'd work for both normal cases & edge cases, however I couldn't.
    The question is: Did you solve this question on your own, if so, how long did it take you to solve it? Second, if not, did you watch someone else's solution & than understand that solution to solve this problem?
    I feel like I'm not good enough since I couldn't solve it on my own & I had to look at other's solutions to figure out a solution. This is making feel worried if I'm good enough for the coding interviews at Big companies like Google, Facebook, Microsoft or Amazon.

  • @erbenton07
    @erbenton07 4 года назад +1

    Why does the array begin with element 1? Shouldn't it begin with element 0?

  • @vsDizzy
    @vsDizzy 4 года назад +1

    Indexes are zero based. There is no need to add 1.

  • @Hogaboga2
    @Hogaboga2 3 года назад +1

    OH MY GOD! So I've been watching your videos and solving these questions for the past 4 hours. I could finally come up with the right solution this time. I'm not so rusty after all! So pumped rn! lol

  • @aryamannningombam2351
    @aryamannningombam2351 4 года назад +1

    This question's difficulty level would increase 69X if it has negative numbers too

  • @Alison-yg6qs
    @Alison-yg6qs 4 года назад +8

    awesome :)!!!!

  • @akashgaur1121
    @akashgaur1121 4 года назад +6

    nicely answered (y)

  • @ahyes5842
    @ahyes5842 4 года назад +3

    Me: See's thumbnail
    My Brain: Ara Ara

  • @jbkhan1135
    @jbkhan1135 4 года назад +3

    Excellent job describing these algorithms! I wish I had videos like these when I first started my career in software engineering!

  • @i_deepeshmeena
    @i_deepeshmeena 4 года назад +1

    Some people think that two nested while loops have time complexity O(n^2). pay attention to the explanation the pointers never go back to the start of the array they just move in one direction so linear time

  • @treyquattro
    @treyquattro 4 года назад +3

    5th Element? That would be Milla Jojovich

  • @ahmedgamberli2250
    @ahmedgamberli2250 3 года назад

    I have got a question. I think your first example was wrong. Index of 2 was 1 in your array. I don't know Java's indexing rules but it's wrong in python. And, I want to publish my python answer:
    def findLongestSubarrayBySum(arr, s):# O(n^2) O(1)
    r = []
    for i in range(len(arr)):
    for j in range(i, len(arr)):
    if sum(arr[i:j]) == s and len(arr[i:j]) > len(r):
    r = [i, j]
    return r

  • @dinhnhobao
    @dinhnhobao 4 года назад +2

    HIGH Quality content!! Gonna watch all your Leetcode qns interview now to help me prepare!!

    • @dinhnhobao
      @dinhnhobao 4 года назад

      Make more of this please! Thanks!!

  • @switchyard
    @switchyard 2 года назад

    Didn't realize this would work as long as it's positive. I thought this approach only works if you sorted the array...
    So if negatives were involved, prefix sum is the answer?

  • @kingsleyusoro6349
    @kingsleyusoro6349 2 года назад

    Could someone please explain to me how you decided to start off from 2,3,4.
    I don’t really understand the concept of the whole contiguous sub array

  • @ctral1991
    @ctral1991 4 года назад +1

    I am thinking can I stop the loop check if the difference of 2 elements in result array is larger than the arr.length - left

    • @total_dk6517
      @total_dk6517 4 года назад

      Assuming I understand you correctly, then in that case, right would be equal to arr.length. And thus the answer must be yes, since you cannot create a longer array.
      Edit: Assuming r != [-1]

  • @georgerussel
    @georgerussel 2 года назад

    Will the while condition work for input like:
    [1,1,1,1000], sum=100
    ?

  • @bohdanbaida
    @bohdanbaida 10 месяцев назад

    Nice problem. Thanks for explaining, Nick.

  • @gabriellerosa6453
    @gabriellerosa6453 4 года назад +1

    Love your work in youtube ! Great algo and good question. Ty you're helping me :2

  • @tanayparmar3020
    @tanayparmar3020 3 года назад

    Find the maximum number after adding n number of comma example 455 and count of comma is. 1 then 4,55 so maxnumber is 55 can you find suitable solution of it

  • @indranildas9565
    @indranildas9565 3 года назад

    Cant we move both the left and right pointers together?! after all the window was longest :/ so just moving the left slider is only gonna make it small which is not a desired result anyways

  • @punstress
    @punstress 4 года назад +1

    If there were negative numbers in the array, you'd have to keep going through using the left pointer, correct? Because you could hit 15 again.

    • @ThanhNguyen-sl2kd
      @ThanhNguyen-sl2kd 4 года назад +4

      if there were negative numbers in the array, this algorithm will not work, you have to use prefix sum. Because this algorithm is base on a rule that if you expand the right -> the sum will increase, if you shrink the left -> the sum will decrease. Unfortunately that's not the case for negative numbers.

  • @davidjames1684
    @davidjames1684 3 года назад

    2:48 - you need to go back to school and learn your powers of 10. 10^5 is not 10,000 and 10^4 is not 1,000.

  • @yahia1355
    @yahia1355 4 года назад

    why the yellow pointer doesn't just start from the end of the array and decreasing it , this will directly get the longest possible subarray

  • @elmirach4706
    @elmirach4706 2 года назад

    r=[1:3], not r[2:4] since it starts from index 0

  • @aj-lan284
    @aj-lan284 3 года назад

    Same Algorithm was in my mind but didn't know how to code😄

  • @liutomzhi
    @liutomzhi 4 года назад +2

    This algorithm works only if the int array are all positive or non negative

    • @NickWhite
      @NickWhite  4 года назад +4

      in the video I say that the elements are all positive

    • @liutomzhi
      @liutomzhi 4 года назад

      got it, got it. My bad. :-) and forgot to mention that all your videos are good, keep up!

  • @nextgodlevel
    @nextgodlevel 4 года назад

    how it's linear, because there is two while loop in it????????

  • @polill00
    @polill00 3 года назад

    I don't understand, I feel there are use cases where this wouldn't work. For example (looking for a sum of 15)
    [5, 11, 10]
    The window wouldn't see any arrays that add up to 15.
    Edit: I see now that it's consecutive elements.

  • @TheAguydude
    @TheAguydude 4 года назад

    A naive nested loop has worse efficiency than you might think. There are O(n^2) ranges calculate sums for. Calculating the sum of those ranges is not O(1), since the length of those ranges is proportional to n. Of course, you can trivially change the naive algorithm to O(n^2) by filling in every possible sum into a 2D array (i.e., by using dynamic programming). That said, the problem constraints guarantee that array members are always >= 0, so the sliding window approach is far superior.

  • @vineetagarwal1305
    @vineetagarwal1305 4 года назад +1

    Very Nice Explanation Nick.......Keep the momentum going on.

  • @RowlandMteteziDigitalsurgeon
    @RowlandMteteziDigitalsurgeon 4 года назад

    This solution, seems to fail for this kind of case:
    arr = a = [-2, -3, 4, -1, -2, 1, 5, -3]
    s = 6
    given the constraints where we are guaranteed only positives. If that were removed, is it possible to tweak this to result in a correct solution too?

    • @dominiquefortin5345
      @dominiquefortin5345 3 года назад

      Your case as no solution, but this code will tell you :
      function findLongestSubarrayBySum (arr, s) {
      var len = arr.length;
      var deb = 0, fin = 0, sum = 0;
      var r = [-1], ln = 0;
      while (deb < len) {
      for (; sum ln) {
      ln = fin-deb;
      r = [(deb+1),fin];
      }
      }
      for (; (sum > s || fin >= len) && deb < fin ;) {
      sum -= arr[deb]; deb += 1;
      if (sum === s && fin-deb > ln) {
      ln = fin-deb;
      r = [(deb+1),fin];
      }
      }
      }
      return r;
      }

  • @anujsavita8912
    @anujsavita8912 3 года назад

    Loved this video!
    But this solution won't work for negative values.
    Example: arr =[-1,-1,1], s=0

  • @chiranthanmr
    @chiranthanmr 3 года назад

    You can stop once the remaining length is lesser than the current longest length.

  • @ibrahimshaikh3642
    @ibrahimshaikh3642 4 года назад +1

    Loved this ,new way of explanation

  • @HaroWorld1
    @HaroWorld1 3 года назад

    This only works if numbers are >0

  • @timowerner6798
    @timowerner6798 3 года назад

    how about sorting the array first, then start with the moving window and return the first found solution? since all the numbers next are bigger, so it cant become longer. worst case is worst here, but average should be (maybe) better?

  • @PavelSenko
    @PavelSenko 4 года назад

    You didn't have to freeze right pointer until you have current_sum equal to given sum (while moving left one further). You could just slide the window for 9 elements to the right until it's less or equal of the given sum. Your goal is to find a window with more than 8, so, why shrink it?

  • @BharathiFromSouth
    @BharathiFromSouth 4 года назад

    Give the tried history or give us link. Main thing is understanding the problem. We fail in that stage.

  • @sunimod1895
    @sunimod1895 3 года назад

    How would you do this if instead of an array/list, it was a set, so find the longest set of numbers that add to the sum (where order doesn't matter)?
    Would you have to store every single set that doesn't add up to greater than S?

  • @davidjames1684
    @davidjames1684 3 года назад

    Since the first 8 array elements sum to 15, you would then only need to check subarrays of length 9 or longer (starting at elements 2, 3, 4, and 5). Once you don't find any of length 9 and all of those length 9 sums are more than 15, you would be done.

  • @shockwave1789
    @shockwave1789 3 года назад

    If one pointer reached end and the distance between the other array is less than current max length we can exit the loop early ?

  • @yaswanthp2294
    @yaswanthp2294 2 года назад

    How do you come up these solutions??

  • @sniff4643
    @sniff4643 3 года назад

    your'e my favorite person on earth

  • @RageshKumar77
    @RageshKumar77 4 года назад +2

    Hey, Bighead:)

  • @Alankid2016
    @Alankid2016 4 года назад +8

    r= [1,3] first element is index 0

    • @elijahbuscho7715
      @elijahbuscho7715 4 года назад +2

      In the problem description it says that the output should be 1-indexed, not 0-indexed (they call it 1-based)

  • @ahmedfattah5879
    @ahmedfattah5879 4 года назад

    can anyone give me the link to this problem ??
    the link that nick gave go to the home page of coding signal .

  • @neilijason07
    @neilijason07 3 года назад

    Love from Taiwan, thanks so much.

  • @zarifmuhtasim
    @zarifmuhtasim 4 года назад +1

    I love this kind of videos.Keep up the good work man

  • @8thFloorHarmony
    @8thFloorHarmony 3 года назад

    liked and subscribed!

  • @EvanAr007
    @EvanAr007 3 года назад

    It seems like single link list, innit?

  • @TechOnScreen
    @TechOnScreen 3 года назад

    You write so clean code than other RUclipsrs out there. Impressed!

  • @PoulJulle-wb9iu
    @PoulJulle-wb9iu 4 года назад

    10^5 is a 100k not 10k, and 10^4 is still not a 1000.... int left n right are not pointers

  • @alifwide1635
    @alifwide1635 4 года назад

    *YO MAN THAT ARRAY IS STARTING FROM [1] THATS ILLEGAL IM CALLIN THE POLICE™*

  • @mayurravindra9433
    @mayurravindra9433 3 года назад +1

    Amazing!

  • @DurgaShiva7574
    @DurgaShiva7574 4 года назад

    Hi Nick..nice job...
    But one important question...
    Doesn't this approach looks like the elements in the array should be sorted...as you are popping out the LHS elements on the sum being greater than S??

    • @Clownvin69
      @Clownvin69 4 года назад +1

      I thought the same thing. I didn't think it would work on an array like [0, 0, ... numbers, 0] since it would likely pop off the initial 0s at some point. But then I thought about what a sub array really means, and its likely the problem doesnt allow you to pick numbers that arent adjacent. Like if you did a slice on the array.

  • @chumaumenze5598
    @chumaumenze5598 4 года назад

    I totally agree. Some of the problem description often make things look more complex than it should. I tend to read them like 3 times to make sure I understand it.

  • @janakiraman5232
    @janakiraman5232 4 года назад

    Is there a possibility where the left pointer > right pointer ?

  • @nareshg7292
    @nareshg7292 4 года назад

    please reply !
    is it necessary to solve the problem in a particular programming language or is it left as a choice ?

  • @joel_8806
    @joel_8806 4 года назад

    Wow what an amazing explanation. Thanks bro !

  • @MundoFinky
    @MundoFinky 4 года назад

    9:02 is that really necessary? Why check the sum, when you already know the current subarray you're checking has less items than the subarray we found earlier, which is 8 items long? No way it will be the longest subarray. Shouldn't you move the yellow arrow instead?

    • @total_dk6517
      @total_dk6517 4 года назад

      You're right. This could be solved by incrementing both pointers instead of only the left pointer if r != [-1] (in the given situation)

  • @pranavpatki
    @pranavpatki 4 года назад

    I understood the optimal solution because you explained it clearly, but couldn't understand the brute force solution 5:33 (you just said "keep going, next one, look at all of em") pls explain brute force solutions clearly too.

    • @total_dk6517
      @total_dk6517 4 года назад

      Bruteforce solution:
      (If at any point the sum of a subarray is equal to s, you have the answer)
      - First check if the sum of the whole array (of length n) is equal to s.
      - Then check if any subarray of length n-1 is equal to s.
      - Then check for any sub
      array of n-2 ...
      - Continue until you've checked all subarrays of size 3, then 2, then 1..
      - If you still haven't found a subarray with sum s, then there is none and you return [-1].

  • @siddarthbali12
    @siddarthbali12 4 года назад

    What if there are negative numbers in the array??

  • @sinamirmahmoud7606
    @sinamirmahmoud7606 3 года назад

    blackjack! hit me

  • @Narper100
    @Narper100 4 года назад

    Hi! What will happen if I try to go from the begin and the end of the array, moving borders to each other while sum inside is not equal to the value (comparing arr[left] and arr[right] and deleting from subarray the biggest)? I'm feeling something will go wrong, but can't catch what.

    • @i_deepeshmeena
      @i_deepeshmeena 4 года назад

      your approach seems O(n^2) to me but can you explain how will to add the elements in a variable if your first pointer is at the start I mean which pointer you are using for adding and which for subtracting the values from sum

  • @ltrboks
    @ltrboks 4 года назад

    If arr=[1,2,3,7,5] and sum = 9 the longest sub array adding up to 9 is [1,3,5] this algorithm will not find it because it only works for contiguous subarrays and it would be beneficial to state so in the problem description in the beginning of the video.

    • @total_dk6517
      @total_dk6517 4 года назад

      I wee where you're coming from, but subarrays can only be continuous. [1,3,5] is not a subarray of [1,2,3,7,5]. Imagine a subarray like being a slice of the array.

    • @dominiquefortin5345
      @dominiquefortin5345 3 года назад

      A sub-array is not a sub-set.

  • @shabbar0333
    @shabbar0333 4 года назад

    instead of storing result as as an array of left and right variables, we can just store the current maximum length of the subarray sum. Then if right - left is greater than result we update result as right-left.

  • @porco-espinhoentediado6258
    @porco-espinhoentediado6258 4 года назад

    Nice solution. Another idea is prefix sum and binary search, but two pointers is much better.

    • @Havok632
      @Havok632 4 года назад

      Porco-espinho Entediado you cannot do binary search since the array is not sorted. The prefix sum will not be sorted either because negatives will decrease the sum

  • @akshatsharmasre
    @akshatsharmasre 3 года назад

    Nicely explained

  • @aishsingh9909
    @aishsingh9909 4 года назад

    I don't know if my solution is more efficient or not but still, here it is :
    Step 1: sort the array in ascending order
    Step 2: loop through the sorted array and keep adding the elements until the sum becomes equal to the given no.
    Step 3: Once the array sum becomes equal to the given number pick the positions and return them.

    • @AnkitKumar-tp8hc
      @AnkitKumar-tp8hc 4 года назад

      Sorting would mess up the order of the array.

    • @Legac3e
      @Legac3e 3 года назад +1

      In addition to what Ankit said: sorting has a best case time complexity of O(n log n).
      So Nick's algorithm would return the correct answer before you even finish with your first step (since Nick's solution is O(n)).
      (Side note: not that I could do any better, either. I only managed to come up with a O(n^2) solution before seeing Nick's solution.)

  • @eldarcivgin6871
    @eldarcivgin6871 4 года назад

    You can also add a contidion that exits the loop if the total length of the array subtracted by the current position is shorter than the length of the subarray we already found, because then we can't find any subarray longer then that already found.

    • @tofahub
      @tofahub 4 года назад +1

      Since the array is not sorted, you may have smaller numbers at the right end of the array which you can add to the sum and larger numbers towards the beginning of the array that you can subtract from the sum. In that case you would have added more numbers than you would subtract contributing to a longer subarray

  • @ibrahimshaikh3642
    @ibrahimshaikh3642 4 года назад

    Loved this new way of ur explanation

  • @amirammar6687
    @amirammar6687 3 года назад

    Thank you 🙏

  • @minhazulislam4682
    @minhazulislam4682 4 года назад

    7:09 I was thinking the same when you said it.

  • @MrAlwaysBlue
    @MrAlwaysBlue 4 года назад

    Are you not using zero indexed array?