Sum of All Odd Length Subarrays | LeetCode 1588 | Explained and Java Code

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

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

  • @OscarMartinez-nt6zn
    @OscarMartinez-nt6zn 8 месяцев назад +1

    This is a second-to-none explanation! It took me a while to wrap my head around this! I'd be surprised if someone is going to be able to come up with this solution in an interview without having seen it before.

  • @TomarSahabVlogs
    @TomarSahabVlogs 4 года назад +24

    it's 5 am IST morning now I can sleep, Thanks

  • @silvahawk
    @silvahawk 3 года назад +21

    Leetcode rates this as an easy problem, man. When I looked at this solution's formula in the discussion without watching this video, I was so lost

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

      The brute force solution is accepted and is easy. The O(N) Solution is straight up HARD

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

      @@mandy1339 brute force is o(n^3). Very bad one

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

      +1

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

      @@TechWithAshishKumar a little bit optimization on brute-force will bring TC to O(N^2).
      Check my code:
      int sumOddLengthSubarrays(vector& arr) {
      int finalAns = 0;
      for(int i=0;i

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

      You can use a prefix sum array and change brute force method to O(n^2)

  • @anon1207
    @anon1207 3 года назад +13

    Great Explanation!
    Spent 3 hours trying to figure out the logic behind (i + 1) *( n - i)
    You cleared it in 6 minutes with that city example.
    Now I can finally sleep after 3 am : ...... )

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

    That is a very good explanation. By the way, you can omit the check for odd by following a simple trick.
    Ex. if the number is ODD; 5 --> (5+1)/2 = 3 . and if it is EVEN; 8 --> (8+1)/2 = 4.
    Keep doing such nice videos.

    • @natesantti906
      @natesantti906  4 года назад +10

      Thanks for the nice comment! You are correct, the "round up" division by 2 also works for this and even makes the code shorter.

  • @fardin_shaik
    @fardin_shaik 4 года назад +12

    The explanation is amazing man. Couldn't have asked for more.

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

    Boom! Marvelous question and marvelous explanation.

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

    This is amazing. You did a REALL GOOD job explaining it and exemplifying it wherever necessary. THANK YOU.

  • @sureshgarine
    @sureshgarine Год назад

    Wow! this is a wonderful explanation. Thanks a ton! It would be great if you do more of this kind of leetcode solution

  • @shivalikagupta3433
    @shivalikagupta3433 Год назад

    It was a great explanation, I derived sum of all even length subarrays based on this intuition. Thanks a ton dude !!

  • @ronniey4231
    @ronniey4231 Год назад

    Such a great explanation! It’s so clear ❤❤❤❤

  • @Mahmoud-AbouDeghedy
    @Mahmoud-AbouDeghedy 2 месяца назад

    Extremely perfect explanation 🎉

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

    Superb explanation. I'll have to re-watch it to really nail down the thought process. You have a great way of dissecting the problem and wrote clean code! Thanks for this.

  • @ShinAkuma
    @ShinAkuma 4 месяца назад +1

    Ain't no way I'm coming up with that in an interview.

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

    Wow. Just wow. Your channel is super underrated and I hope you grow way bigger because it's definitely well deserved

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

    Amazing explanation! Many thanks! Keep doing what you're doing.

  • @hariomtiwari7605
    @hariomtiwari7605 Год назад +1

    Nice Explanation

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

    Really great explanation Nate! Was able to figure out the brute force solution but this one is really elegant

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

    Explanation was Fire 🔥🔥🔥

  • @KostiantynNosach
    @KostiantynNosach Год назад

    Grate explanation!!!! I got it on the middle of video👍

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

    Please continue posting....your way of solving problem is really helpful

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

    This solution is just more than perfect!
    I am just wondering how deep you went finding it!!!

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

    This is an incredible explanation brother !!! Keep it up.

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

    Phenomenal,
    Hoping to see more videos from you
    Thanks a lot

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

    Thanks for explaining thought process behind the solution.

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

    Your videos are indeed amazing . Please keep uploading...

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

    Really well explained man. Lots of thanks you saved much of my time.

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

    Thank you for your video, it is a great explanation, for an interview I think I'd go for the N2 approach first and if I have time I'd go for this one as I find it harder to explain, by the way if anyone is doing it in JavaScript remember to use Math.floor when dividing.

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

    This is mind-blowing. Thanks.

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

    I can't thank you enough. 🙌

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

    Simple and easy explanation, Great!

  • @NateFreestyle
    @NateFreestyle 2 месяца назад

    Fantastic explanation!

  • @spardha-yug
    @spardha-yug 2 года назад

    I appreciate your efforts.
    Really very good clarification of complex concept.
    subscribed :)

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

    best solution for beginners thank you 😄

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

    Thanks for this clear explanation. Goog Job ;)

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

    Thank You. The explanation was very good.

  • @ashishjain871
    @ashishjain871 11 месяцев назад

    It still isn't clear why multiply at time 5:30? The arrays starting at index i are different from arrays ending at index i so how does it make sense to multiply them?

  • @chiragsingh8323
    @chiragsingh8323 Год назад

    Really great explanation❤

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

    Why do we use paths to represent (n-i) and (i+1) as opposed to just adding them?

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

    Bruh this is very helpful. Thank you.

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

    Thanks a lot bro:-) Much needed, deserves subscription:)

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

    could not get any better than this

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

    thanks a lot. your video helped me so much in understanding this solution.

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

    thanks for saving my day. 😊

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

    Amazing explanation. Thank you.

  • @malamelliali6115
    @malamelliali6115 Год назад

    at minute 7:22 how is the answer 4 you said (1,2) ( 1,2,3) (1,2,3,4) shouldnt it be 3 ? or we add (1) so it becomes 4 ?

  • @p.jayaraman2742
    @p.jayaraman2742 3 года назад +1

    Great explanation Thank you 😊👍

  • @Jabkevich
    @Jabkevich 4 месяца назад

    bro, you are the best

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

    That's really a great explanation..thanks

  • @RahulPawar-ok3nc
    @RahulPawar-ok3nc 3 года назад

    Great Explanation bro!!!!!!!😁😁😁😁

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

    excellent solution

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

    really good explanation

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

    i did not understand the even and odd part ..could someone explain plz..after the total how did he get the odd ones

  • @Kyle-ho4lj
    @Kyle-ho4lj 4 года назад +2

    thank you for the solution and video.

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

    Kudos to the amazing explanation. But still wondering how did you come up with this solution.

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

    The no of ways from A - > C analogy was explained in a brute force way. I couldn't understand why would we consider counting number of ways an element starts in a subarray and ends in a subarray? can you please explain?

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

      +1 . he just jumped right into saying that the number of occurrences for a val to appear in all possible sub-arrays is the number where val appear at start * val appear at end . but i want the reasoning as well , this is more like an observation of how to related the total count of occurrences of val in all sub-arrays to some kind of formula which is frustrating to me

    • @minhquang1796
      @minhquang1796 25 дней назад

      ​@@saeedjinat9173 This is my understanding (it's a bit late if you have come up with any new ideas, i am happy to learn)
      The idea is to multiply the number of occurrences of each number in all the odd-length subarrays. The question is how to do that. In this video, the sub-problem is "How to count a number's occurrence in all possible subarrays". He divides the subarrays into two parts: Start with a number and End with a number (yes, think about it)
      For instance: how can we count how many numbers surrounding '3'
      1 2 3 4 5
      Instead of doing brute force, like counting layers: 2 4 -> 1 5
      We count by sides: left: 1 2 (ends with 3), right: (start with 3) 4 5 -> sum them up: 4
      Dividing that way won't make the two-part overlap, making it easier for calculating (just sum or multiply them up).

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

    Thank you. Very well explained.

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

    Wonderfull Thank you so much!

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

    Excellent video

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

    This is genius!

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

    Really great observation and problem solving approach for this question! How did you come up with this intuition at the first place? And why does Leetcode marks it as Easy lol! :D

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

    Deserver to be subscribed but don't understand one thing why the hell leetcode call it easy problem

  • @Noone-kl6sc
    @Noone-kl6sc 3 года назад

    awesome bro

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

    Awesome !!

  • @vibhavaribellutagi9738
    @vibhavaribellutagi9738 Год назад

    Great video
    How did you come up with the solution? seems like its not any pattern?

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

    Great explanation, thanks

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

    awesome explanation

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

    just awsm!! explanation.....btw why did you stopped making contents?

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

    One quick question is, how could we prove that subarr start * subarr end = the occurence of that number in the array. I felt confused at that part. The graph you drew kinda make sense, but its still a bit abstract for me to understand.

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

      He explained in reply to another comment.

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

      yea it is a bit complicated.....think about it for some while....watch the video again from that part...you'll get it.

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

    Can you explain your thought process on solving these kind of problems, Its really hard to get an proper idea about a better solution.

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

    Amazing video

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

    Great Video! But not sure why it's marked as Easy on LeetCode..

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

      Thanks! I think it's marked easy because the brute force solution that runs in O(n^3) works. There is a sliding window solution that runs in O(n^2) too. They could easily have the same question but with a larger input to force a more creative solution and then mark it as medium.

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

    we can simply divide (total+1)/2 simple there is no need to put if statement

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

    why is half of the subarrays be odd size and the half be even size?
    i don't understand why you skip it tho. it doesn't sound trivial to me.

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

    Hi, great video!
    Your method is very interesting although it`s kinda hard to understand, why total number of subbarays = start * finish.

    • @natesantti906
      @natesantti906  4 года назад +15

      Thank you! That is the hardest part of the problem to understand. If you think about that city example of how many paths there are from A to C going through city B, then pretend city B is the index we are interested in.
      If there are A subarrays ending where we are at (B), and C subarrays beginning where we are at (B), then the total times B appears in subarrays is A*C.
      Here's an example: arr = [4,8,5,6,1]
      Let's look at how many total subarrays index 2 (value 5) is in. Index 2 will be our B.
      There are 3 subarrays that end with 5: [4,8,5] [8,5] and [5]. We'll call these 3 subarrays A.
      There are 3 subarrays that start with 5: [5] [5,6] and [5,6,1]. These 3 subarrays will be C.
      Looking at the first subarray in A [4,8,5], we can add every combination in C to it.
      [4,8,5] with [5] gives [4,8,5]
      [4,8,5] with [5,6] gives [4,8,5,6]
      [4,8,5] with [5,6,1] gives [4,8,5,6,1]
      Notice how all 3 subarrays of C were added to 1 of the subarrays of A? This is possible because they can be connected with B.
      Repeat the process for the second subarray in A [8,5]. Add every subarray in C to the second subarray in A.
      [8,5] with [5] gives [8,5]
      [8,5] with [5,6] gives [8,5,6]
      [8,5] with [5,6,1] gives [8,5,6,1]
      3 more subarrays were created with B as the connecting index.
      Repeat once more for [5] (the last subarray in A).
      [5] with [5] gives [5]
      [5] with [5,6] gives [5,6]
      [5] with [5,6,1] gives [5,6,1]
      The grand total subarrays here was 3*3 = 9.
      I recommend drawing out an example because I find that helps it click more for me.

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

      ​@@natesantti906 thank you for clarification. It really helped me understand solution much better!
      Keep up the good work!

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

      @@natesantti906 thanks a lot, I finally understand

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

      @@natesantti906 Thanks a lot this was very intuitive

    • @seikimaiihuang3382
      @seikimaiihuang3382 Год назад

      @@natesantti906 thank you for clear explanation! OMG!

  • @armansaini5500
    @armansaini5500 7 месяцев назад

    thanks!

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

    I beg to differ on the formula for calculating number of sub arrays!!!! Number of subarrays=n-i while the total number of element occurrences=(n-i) *(i+1).

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

    Great explanation, it's not in any way easy question.

  • @Hariharan0606
    @Hariharan0606 Год назад

    wow

  • @mdshajjadulferdous8057
    @mdshajjadulferdous8057 9 месяцев назад

    Great explanation