Frequency of the Most Frequent Element | Binary Search | Sliding Window | META | Leetcode-1838

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

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

  • @newglobal2056
    @newglobal2056 Год назад +23

    College ke computer lab me apka lecture dekh raha hu, ha teacher se permission lia hai maja agaya bhaiya ❤❤❤

  • @YashSharma-fs3mn
    @YashSharma-fs3mn Год назад +4

    Yaar bhaiya kaise itna achhe se soch lete ho aap, Mere liye to kaafi jyada tough hai ye sab. Par itna achha samjhate ho aap majha aa gya

  • @dhairyachauhan6622
    @dhairyachauhan6622 Год назад +7

    this was indeed a good problem, definately on the medium hard side, i am happy that i learned something new today

  • @oqant0424
    @oqant0424 Год назад +11

    The thought process u explained in the start , I was able to think of it by myself
    So proud of myself ..........all thanks to your story to code process😇😇

  • @Zomb-zj4ip
    @Zomb-zj4ip 5 месяцев назад +2

    So many videos on this topic, but the only one that even remotely helped me understand it completely was this one. Thank you so much for taking all the examples to explain, running each dry run explaining everything in depth. Youve earned yourself a subscriber. Thank you again!!!

  • @raynwani137
    @raynwani137 3 месяца назад +2

    Bhai bohot bohot shukriya ! Bohot pyaar se samjaya!

  • @bash-ian
    @bash-ian 4 месяца назад +2

    Bhaiya you're the best, I suck even on 800 codeforces problems, I've gave up 2 time learning DSA, now I've started from today onwards again just because of your simple explanation made me believe I too can do it. I'm blessed to find your channel.
    Thanks a lot bhaiya. Lots of love and support (:

  • @balakrishnanr648
    @balakrishnanr648 Год назад +5

    42:14 - There is a mistake
    You cannot make all 3 guys in the window as 4. 4 + 4+ 4 = 12 NO.
    As you cannot decrement 13 to 4.
    Only way is to make every guy as max guy in that Window.
    So make all as 13.
    13*3 = 39 is what new window sum.
    ALGO CORRECTION: To get the MAX - then we to sort it. Then only target = Nums[r] works.
    Just a correction. Kindly mention it or pin the comment for others help.
    Orelse explaination is awesome. Really the first point - making to a element that is already in array. That doubt I had really on my mind. Why not to any random value.
    Cleared it like a smooth knife cut on a butter bar.

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

      Yes yes, let me add that in the caption.
      Thank you 🙏😇

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

      I have added a small dry-run correction caption from 38:35 (might reflect after a while). Click on "cc" to display them on screen. Thanks a lot 🙂 (Just needed to sort the array)

  • @harshitgarg2820
    @harshitgarg2820 Год назад +3

    It's very mind troubling question but you made it look so easier. I've just subscribed to your channel and you gave solutions to lc daily questions which make them look like a piece of cake😅

  • @JoyAcharyatvl
    @JoyAcharyatvl Год назад +3

    bhai learned lot of things from this video. thanks.

  • @YashMalav-kh1ov
    @YashMalav-kh1ov 8 месяцев назад +2

    I did this ques earlier by learning from another utube channel but he just presented the formula no intuition but the way you explained right from binary search and then sliding window i must say what u wrote in caption-I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY. u meaned it men!!!! thank u ❤❤

  • @study-yd6es
    @study-yd6es 2 месяца назад +1

    keep uploading these type of lecture sir

  • @unknown47896
    @unknown47896 5 месяцев назад +3

    hatsoff to ur fist apprach bro..

  • @OIAOa
    @OIAOa Год назад +4

    Your doing a lot of hardwork for us ....❤

  • @akshayrahangdale8511
    @akshayrahangdale8511 9 месяцев назад +1

    Very Nicely Explained.

  • @tanishasethi7363
    @tanishasethi7363 7 месяцев назад +2

    Loved the explanation 🤌✨️

  • @anshukmishra4520
    @anshukmishra4520 Год назад +2

    I love watching your videos. Doing great bhaiya.

  • @KshitijPratyush
    @KshitijPratyush 13 дней назад +2

    Simplicity at its best. Thanks Buddy.
    Is there any list of problems (medium to hard) you have made videos on? Would like to watch them.
    Please do let me know if there are any such lists available where you have created videos on.

    • @codestorywithMIK
      @codestorywithMIK  13 дней назад

      Let me know if this helps - docs.google.com/document/u/0/d/1Hqdu5bivW6kHBSr-hGyvO2kYOKkyTtM3qmhmHYH7s7o/mobilebasic
      I will soon add more to this doc
      Thank you 🙏♥️

    • @KshitijPratyush
      @KshitijPratyush 12 дней назад

      @@codestorywithMIK Thank you so much. I will watch your videos and start practicing.

  • @Abc-rz4ps
    @Abc-rz4ps 4 месяца назад +1

    Ur voice is cute Bhaiya 😊

  • @Anonymoussssss-l6r
    @Anonymoussssss-l6r Год назад +2

    very nice explanation

  • @anuppatankar4294
    @anuppatankar4294 Год назад +2

    Great Explanation 🔥

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

    Incredible content

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

    very nice explnation sir

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

    Thank u so much...........learnt a lot 💖💖

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

    Amazing explanation, I like these in-depth videos, teaches me a lot.

  • @rolls423
    @rolls423 Год назад +2

    thank u so much

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

    Very nicely explained ❤

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

    👌🏻👌🏻👌🏻That’s pure Art

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

    best of best , keep doing ❤

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

    intuition is good, it would be better if you said array is sorted before applying the sliding window in 1st sliding window example.

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

      Yes, my bad I missed that. I have added a small caption for correction. Thank you 😇🙏

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

      I have added a small dry-run correction caption from 38:35 (might reflect after a while). Click on "cc" to display them on screen. Thanks a lot 🙂 (Just needed to sort the array)

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

    Again THE best 😊

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

    ur consistency is an inspiration💖💖

  • @codestorywithMIK
    @codestorywithMIK  Год назад +4

    Hi everyone,
    I have added a small dry-run correction caption from 38:35 (might reflect after a while). Click on "cc" to display them on screen. Thanks a lot 🙂 (Just needed to sort the array)

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

    public int maxFrequency (int[]nums,int k){
    int ans=0;long sum=0;
    Arrays.sort (nums);
    for(int i=0,j=0;j

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

    The solution cleared 57 test cases but gave me tle:
    class Solution {
    public:
    int maxFrequency(vector& nums, int k) {
    sort(nums.begin(),nums.end());
    int result = 0;
    for(int i = nums.size()-1; i != 0; i--){
    int j = i-1;
    int count = 0;
    int temp = k;
    while(j >= 0 and temp != 0){
    temp = temp - (nums[i]-nums[j]);
    if(temp < 0){
    break;
    }
    count++;
    j--;
    }
    result = max(result,count);
    }
    return result + 1;
    }
    };

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

    Amazing ❤❤❤❤

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

    Thankyou sir

  • @room5079
    @room5079 9 месяцев назад +2

    if we have array like [1,1,1,4] and k=3
    then we can have 2 as max frequency instead of 4.?????

  • @simnidhnidh9122
    @simnidhnidh9122 3 месяца назад

    can i directly jump to sliding window soln and skipping that binary wala sol becoz that seems to be difficult?

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

    Bhai jab long long aur int type ke variables ko use karke koi operation/expression perform karte hai to us expression ka result long long hota hai ya int. Please help me out. Great explanation with intuition. Learned new concepts from your video

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

    1st approach sochane ke liye kitna time laga? do you solved any same question before ? becoz that binary search formulae (count * target) and operation = windows - currsum ; that doesn’t come directly into mind. so give me some advice !!
    nice approach first one you did it on first attempt. 💥

  • @Farhan-jj2gn
    @Farhan-jj2gn 2 месяца назад

    sir, if -1e5

  • @md.tamaltahasinkhan6448
    @md.tamaltahasinkhan6448 Год назад +4

    Just solved the question then start witching your video. Surprisingly I came up with the 2nd approach. Although it takes 2 long hours for me to build the intuition. But I think your process of your explanation in previous videos helps me a lot. Specially this video: ruclips.net/video/JrG4tbq6efg/видео.html

  • @tryin_to_code
    @tryin_to_code 3 месяца назад

    bhai har video smjh aajati hai almost aapki, iss baar sachmein kuchhh smjh nhi aaaya, sach mein

    • @codestorywithMIK
      @codestorywithMIK  3 месяца назад

      Apologies if you felt so.
      Would you share what part was confusing, i will try my best to explain it here ❤️

    • @tryin_to_code
      @tryin_to_code 3 месяца назад

      @@codestorywithMIK aree why apologies, I was just informing in a friendly student teacher way, ye ques hi bohot confusing hai, but koini maine rattan laga liya, mujhe intuition itna achhe se kick nhi kar rhi thi

  • @ashishdhal4614
    @ashishdhal4614 Год назад +2

    Solved it only after seeing the sliding window tag 😢

  • @kamranwarsi12b22
    @kamranwarsi12b22 Год назад +2

    instead of prefix sum can't we use inbuilt stl function for computing sum ?? Will it be good or not ?

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

      Accumulate?

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

      @@ru45098 yeahh that one only

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

      Actually accumulate will internally take O(n) time. That’s why prefix sum array is considered.

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

      @@codestorywithMIK ohkk , understood

  • @pankajbhanwala-
    @pankajbhanwala- Год назад +2

    sir 41:00 dry run of example {1, 8, 13, 4} is not correct becaue the nums is not sorted. 41:52 when right pointer reaches 4, you calculated the number of operations required to change 8, 13 to 4 but 8 and 13 cannot be changed to 4 cause we can only increment the value. Although the code was correct because we already sorted the array before.

    • @codestorywithMIK
      @codestorywithMIK  Год назад +2

      Yes yes. Small correction there.
      But i hope the explanation was clear.
      Thanks Pankaj 😇🙏

    • @pankajbhanwala-
      @pankajbhanwala- Год назад

      explanation is great as always ❤️

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

    Hey, I am getting runtime error for the following testcast : [40,95,44,37,41,52,07,52,87,64,40,14,41,33,12,34,80,07,80,44,10,9]
    7925
    Could you help please

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

    class Solution {
    public:
    int maxFrequency(vector& nums, int k) {
    int n=nums.size();
    sort(nums.begin(),nums.end());
    vector dif(n-1);
    for(int i=0;i

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

    Longest video I have watched

  • @RahulKumar-x6m2k
    @RahulKumar-x6m2k 11 месяцев назад +1

    What if , we can do both increment or decrement

  • @baibhav1559
    @baibhav1559 3 месяца назад

    i didn't get how the target will always be in the array what if the, the array is [2, 2, 2, 2, 2, 4, 5 ,6] and k = 5, in this case target should be 3 but 3 is not present in the array

    • @ritesh_3131
      @ritesh_3131 3 месяца назад

      In the question it is already stated that you can apply at most K operations means ,we can apply operations but less than K times ,in above example if you keep target as 2 and if you will not apply any operations means you are not incrementing any element by one in that case max frequency still will be 5 times as 2 appears 5 time in array,and it can be treated as target element,
      Now guess in above same example what will be output if input is 2,2,2,2,2,4,5,6 and k=4 ans still will be 5 as 2 appears 5 times and it can be treated as target.

  • @rajshiromani727
    @rajshiromani727 6 месяцев назад

    god level explaination in very sweet voice👌(●'◡'●)
    mzaa aa gya

  • @xiaoshen194
    @xiaoshen194 Год назад +2

    Acha agar exactly k elements hota to kaise tweak krenge?

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

    can you explain in java sorting ka sc=logn count karta hai?

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

      if you talking about sc then no , your time complexitey will nlogn

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

    bhaiya how can i thank you???

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

      When you get placed, give me a small treat of “kulhad waali chai” 😇
      Keep working hard 🙌😇

  • @shivamkumarkashyap9273
    @shivamkumarkashyap9273 5 месяцев назад

    Help! I'm unable to pass all test cases (Java)
    class Solution {
    public int maxFrequency(int[] nums, int k) {
    Arrays.sort(nums);
    int n = nums.length;
    int result = 0;
    int i = 0;
    int currSum = 0;
    for (int j = 0; j < n; j++) {
    long target = nums[j];
    currSum += nums[j];
    if ((j-i + 1) * target - currSum > k) {
    currSum -= nums[i];
    i++;
    }
    result = Math.max(result, (j - i + 1));
    }
    return result;
    }
    }

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

    The binary solution was explained really well. However, the sliding window explanation was non intuitive.

    • @codestorywithMIK
      @codestorywithMIK  Год назад +2

      Hi there,
      Thank you so much for the precious feedback.
      I would be glad if you can point out at what point you felt non-intuitive for sliding window ?
      I will definitely try my best to clear that out.
      Thanks again ❤️❤️🙏🙏😇😇

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

    bhaiya mera ye solution dekhna maine greedy approach apply ki h 11th testcase pr fail ho rha h can you tell me why ?
    class Solution {
    public:
    int maxFrequency(vector& nums, int k) {
    sort(nums.begin(), nums.end());
    int i=0; int j = nums.size()-1;
    while(i < j){
    if(nums[j] - nums[i]

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

      Can you share the test case

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

      @@codestorywithMIK
      Nums =
      [9930,9923,9983,9997,9934,9952,9945,9914,9985,9982,9970,9932,9985,9902,9975,9990,9922,9990,9994,9937,9996,9964,9943,9963,9911,9925,9935,9945,9933,9916,9930,9938,10000,9916,9911,9959,9957,9907,9913,9916,9993,9930,9975,9924,9988,9923,9910,9925,9977,9981,9927,9930,9927,9925,9923,9904,9928,9928,9986,9903,9985,9954,9938,9911,9952,9974,9926,9920,9972,9983,9973,9917,9995,9973,9977,9947,9936,9975,9954,9932,9964,9972,9935,9946,9966]
      K = 3056

  • @sdmfslkdm
    @sdmfslkdm Год назад +2

    very bad recursive approch , TLE
    class Solution {
    public:
    int memo[10001];
    int dp( vector&nums , int k , int ind , int prev ){
    if( ind >= nums.size() or k = 0)
    pick = 1 + dp( nums , k - (nums[prev] - nums[ind]), ind + 1 , prev );
    notPick = dp( nums , k , ind + 1 , prev);
    return max( pick , notPick );
    }
    int maxFrequency(vector& nums, int k) {
    // greedy doesn't work , atleast for now
    // lets just tur dp , there might be some subproblems

    memset(memo , -1 , sizeof(memo));
    sort(nums.begin() , nums.end() , greater());
    int ans = INT_MIN;
    for( int i = 0 ; i < nums.size() ; i++){
    ans = max(ans , dp(nums , k , i , i));
    }
    return ans ;
    }
    };