Maximum Gap | Leetcode 164 | Live coding session | Leetcode Hard

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

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

  • @MrACrazyHobo
    @MrACrazyHobo 3 ปีที่แล้ว +9

    Thank you for drawing everything out, makes it much easier to understand

  • @MAHALAKSHMIVEERARAJ
    @MAHALAKSHMIVEERARAJ 6 วันที่ผ่านมา

    Great explanation! Clearly understood the approach. Thanks a lot!

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

    Epic dedication brother... First I saw your Maximum product of word length solution (27 may) then N queens 2 and now today... I saw your github profile... Your consistency is just on another level... Ab toh bhai question dekh ke algo ka yaad aajata hoga aapko!!

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

      Most welcome Akshay, haha

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

      @@CodeWithSunchitDudeja suppose 1 backet contains two value min and max value whose difference is max(max gap).....u are considered this case....plz help me

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

      I understand that why are u taken this case....bcz alteat 1 backet is empty

    • @saunaknandi1814
      @saunaknandi1814 2 ปีที่แล้ว +1

      @@CodeWithSunchitDudeja can u share ur LinkedIn Profile

    • @CodeWithSunchitDudeja
      @CodeWithSunchitDudeja  2 ปีที่แล้ว +1

      @@saunaknandi1814 Linkedin: bit.ly/3n65udU this is of Coding Decoded, my personal is www.linkedin.com/in/sunchitdudeja/

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

    Great explanation for this difficult question! Honestly I can't think of ways I can solve it since I know little about the pigeon hole theory. It looks more like a math problem than coding problem to me.

  • @AP-eh6gr
    @AP-eh6gr 3 หลายเดือนก่อน

    if you replace 21 with 17, and 25 with 28, the bucket for 17 remains same at 2, bucket for 28 remains same at 3, and bucket 1 remains empty. But the answer changes to being obtained from two adjacent buckets, #2 and #3 and not #0 and #2 ie the two buckets that skipped an empty bucket in between. so how did the pigeon hole thing help?

  • @abhishekshimpi214
    @abhishekshimpi214 4 หลายเดือนก่อน

    Thank You So much you cleared this concept 🙏👍

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

    at 12:22 the line 41 to 48 is confusing. we just need maxOfBucket[i] - minOfBucket[i] and compare it with the current max value, right?

  • @LegitGamer2345
    @LegitGamer2345 3 ปีที่แล้ว +1

    waiting for todays problem !! search suggestions system , did it using trie , but apparently there are more optimal ways to do it (and i cant wrap my head around those :P )

  • @Vadya-nq8mg
    @Vadya-nq8mg ปีที่แล้ว +1

    Isn't N-2 confusing? What if you have multiple repeating max elements? In your example, what if you had two 49 and two 3s?

    • @shaantanumishra3145
      @shaantanumishra3145 4 หลายเดือนก่อน

      I guess duplicates are not real contender of answer , since max diff it can give is zero , so its doesn't matter

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

    02:43 will the number of buckets always be n-1 ? if n=1000, then number of buckets will be 999?

  • @רועישבח
    @רועישבח 3 ปีที่แล้ว +1

    very good and detailed explanation! Thanks!

  • @ibrahimshaikh3642
    @ibrahimshaikh3642 5 หลายเดือนก่อน

    Maza aa gaya, very clear code

  • @swapaher
    @swapaher 3 ปีที่แล้ว +4

    How would you come up with this approach or intuition?.

    • @CodeWithSunchitDudeja
      @CodeWithSunchitDudeja  3 ปีที่แล้ว +6

      Honestly, it was one of the contest that this question came. I could not solve it then although I was aware of pigeonhole. Then I solved it, so that it somewhere in my head how to go about it. These kind of questions are very difficult to come up with in interviews if you have not solved them before, as they require special tricks to get solved.

    • @swapaher
      @swapaher 3 ปีที่แล้ว +1

      @@CodeWithSunchitDudeja Yes, that's what I was also thinking. Thanks for replying.

    • @neelesh2023
      @neelesh2023 3 ปีที่แล้ว +1

      @@CodeWithSunchitDudeja it would be better, if you tell how did you reach upto solution in starting of the video, that'll make more sense.

  • @HarshGupta-wt9vh
    @HarshGupta-wt9vh 3 ปีที่แล้ว +2

    maximum gap will be no smaller than ceiling[(max - min ) / (N - 1)]. , WHY??

    • @garrub3991
      @garrub3991 3 ปีที่แล้ว +1

      Ceil((max - min / (n - 1)) provides the maximum gap in a scenario where all elements are evenly spaced. If any elements (other than max and min) were shifted in either direction, the max gap would only grow. Thus, there can be no max gap smaller than the max gap for evenly spaced elements.

    • @HarshGupta-wt9vh
      @HarshGupta-wt9vh 3 ปีที่แล้ว

      @@garrub3991 thnx buddy

  • @manoor0858
    @manoor0858 25 วันที่ผ่านมา

    woww such a cool appraoch

  • @LegitGamer2345
    @LegitGamer2345 3 ปีที่แล้ว +1

    can also be done in linear time and space by radix sort

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

    Bro what if there are 2 copies of max element in the array...then how will be the bucket of that element will be found? Because we arr taking only open interval at the closing side

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

    why not use 7 buckets to contain 8 numbers? why exclude min and max

  • @ojasvichaudhary8724
    @ojasvichaudhary8724 3 ปีที่แล้ว +1

    if we create a big array and place number according to their index for ex 3,6,9,1 for this our array will look like 0,1,0,3,0,0,6,0,0,9,0,0..... and we simply ignore 0 and find the difference ?

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

      Of course you can do it but that won't work as the range of input no is 10^9. Hashing solution will cause TLE

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

      Ohh yess .... Thanks 👍

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

      @@CodeWithSunchitDudeja and if we have multiple min and max will it handle that case also (3,3,3,21,9,37,49,49)

    • @CodeWithSunchitDudeja
      @CodeWithSunchitDudeja  3 ปีที่แล้ว +1

      @@ojasvichaudhary8724 yes it will. Run it via a test case

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

    It's amazing you got a new subscriber

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

    Why are we taking an extra bucket??

  • @MDSOHEL-fk3rz
    @MDSOHEL-fk3rz 3 ปีที่แล้ว +2

    That's something bro

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

    @Coding Decoded , can you please explain me by the last bucket will never be empty , since ans will come from right and left side of the empty bucket , this means 1st bucket and last bucket is never empty , can you please explain me this ???????????

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

    Bro everything is well explained can you please tell why did we ignore max and min and why did we choose n-1 buckets and n-2 elements what about one empty bucket?

    • @ysf4049
      @ysf4049 2 ปีที่แล้ว +1

      You can include min, max in n-1 buckets but there is no use, because min value will be in first bucket, max value is in last bucket, while calculating gap b/w two index we need max value of first and min value of 2nd, min is always min for first and wise versa for max

  • @HarshGupta-wt9vh
    @HarshGupta-wt9vh 3 ปีที่แล้ว

    well in questions like these, how will i came to know about what bucket size should i take??

  • @KurumiddeKezia
    @KurumiddeKezia 4 หลายเดือนก่อน

    Bro good explanation but code thopatu example chesedi vunde code antha understand kaledu bro concept ok😢

  • @tathagatnegi5923
    @tathagatnegi5923 3 ปีที่แล้ว +1

    Thnaks alot

  • @deepjyotidebnath4122
    @deepjyotidebnath4122 3 ปีที่แล้ว +1

    Nice explanaition bro

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

    Why we skipped max and min while filling the buckets?

    • @CodeWithSunchitDudeja
      @CodeWithSunchitDudeja  3 ปีที่แล้ว +1

      Because we are trying to put n-1 numbers in n-2 buckets

    • @CodeGenAI
      @CodeGenAI 3 ปีที่แล้ว +1

      @@CodeWithSunchitDudeja Thanks, This video has best explaination. Finally able to solve, but still one question remains - "How to identify if pegionhole principle applies to the problem"

  • @santoshvarma996
    @santoshvarma996 3 ปีที่แล้ว +1

    hi
    can you please tell me why have you not considered the min and Max values of the array?

    • @CodeWithSunchitDudeja
      @CodeWithSunchitDudeja  3 ปีที่แล้ว +1

      So that I can apply pigeonhole principle of placing N-2 elements in n-1 buckets.

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

      hi let's say the Max difference is between the highest element and the second largest element. we are not comparing these values right?

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

    Hey ! Would the use of priorityqueue be considered a valid solution here ??

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

    Hello, Can you please explain leetcode 837: New 21 game please. It will help a lot. Have tried but not getting. Thanks.

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

    Video quality is low, can't see properly

  • @Om-fi3cf
    @Om-fi3cf 8 หลายเดือนก่อน

    how is this O(n)? In worst case this is O(nlogn) only

  • @SurajYadav-ve1tr
    @SurajYadav-ve1tr 3 ปีที่แล้ว

    Bro i've solved this question by my own and got your videos notification so i thought lets have a look but the way thought is totally different i didn't even know that it comes under pegion hole principle. I think this question should not be considered in hard problem its must be in easy section.

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

      The question has a condition to be solved in O(n)

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

      Did you just sort it ... that's funny ..