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

Sdílet
Vložit
  • čas přidán 16. 11. 2023
  • Whatsapp Community Link : www.whatsapp.com/channel/0029...
    This is the 13th Video on our Sliding Window Playlist
    In this video we will try to solve a very good sliding window problem - Frequency of the Most Frequent Element (Leetcode -1838).
    We will also solve it using Binary Search. In total there will be 3 different ways we will solve this problem.
    I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Problem Name : Frequency of the Most Frequent Element
    Company Tags : META
    My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
    Leetcode Link : leetcode.com/problems/frequen...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
    Subscribe to my channel : / @codestorywithmik
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    Twitter : / cswithmik
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook

Komentáře • 93

  • @newglobal2056
    @newglobal2056 Před 8 měsíci +19

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

  • @YashSharma-fs3mn
    @YashSharma-fs3mn Před 8 měsíci +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 Před 8 měsíci +5

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

  • @Zomb-zj4ip
    @Zomb-zj4ip Před měsícem +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!!!

  • @harshitgarg2820
    @harshitgarg2820 Před 8 měsíci +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😅

  • @oqant0424
    @oqant0424 Před 8 měsíci +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😇😇

  • @codestorywithMIK
    @codestorywithMIK  Před 8 měsíci +3

    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)

  • @JoyAcharyatvl
    @JoyAcharyatvl Před 8 měsíci +3

    bhai learned lot of things from this video. thanks.

  • @bash-ian
    @bash-ian Před 27 dny +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 (:

  • @unknown47896
    @unknown47896 Před měsícem +3

    hatsoff to ur fist apprach bro..

  • @YashMalav-kh1ov
    @YashMalav-kh1ov Před 4 měsíci +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 ❤❤

  • @ohiogozaimasu
    @ohiogozaimasu Před 8 měsíci +1

    Sir aapki wajah se me bhi consistent ho gya hu coding me, thanks for making me consistent.

  • @anuppatankar4294
    @anuppatankar4294 Před 8 měsíci +2

    Great Explanation 🔥

  • @Abc-rz4ps
    @Abc-rz4ps Před 26 dny +1

    Ur voice is cute Bhaiya 😊

  • @sukomal07
    @sukomal07 Před 8 měsíci +1

    best of best , keep doing ❤

  • @rohitshah8904
    @rohitshah8904 Před 8 měsíci +1

    Very nicely explained ❤

  • @akshayrahangdale8511
    @akshayrahangdale8511 Před 6 měsíci +1

    Very Nicely Explained.

  • @oqant0424
    @oqant0424 Před 8 měsíci +1

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

  • @satwikghildiyal402
    @satwikghildiyal402 Před 8 měsíci +1

    Incredible content

  • @mohithadiyal6083
    @mohithadiyal6083 Před 8 měsíci +1

    Again THE best 😊

  • @OIAOa
    @OIAOa Před 8 měsíci +4

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

  • @user-kj7eh9ke5n
    @user-kj7eh9ke5n Před 8 měsíci +2

    very nice explanation

  • @ugcwithaddi
    @ugcwithaddi Před 7 měsíci

    👌🏻👌🏻👌🏻That’s pure Art

  • @infinitygaming7192
    @infinitygaming7192 Před 8 měsíci +1

    very nice explnation sir

  • @rolls423
    @rolls423 Před 8 měsíci +2

    thank u so much

  • @tanishasethi7363
    @tanishasethi7363 Před 3 měsíci +2

    Loved the explanation 🤌✨️

  • @tutuimam3381
    @tutuimam3381 Před 8 měsíci +1

    Amazing ❤❤❤❤

  • @balakrishnanr648
    @balakrishnanr648 Před 8 měsíci +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  Před 8 měsíci

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

    • @codestorywithMIK
      @codestorywithMIK  Před 8 měsíci +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)

  • @reshusingh3558
    @reshusingh3558 Před 8 měsíci +1

    Thankyou sir

  • @anshukmishra4520
    @anshukmishra4520 Před 8 měsíci +2

    I love watching your videos. Doing great bhaiya.

  • @sauravchandra10
    @sauravchandra10 Před 8 měsíci +1

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

  • @oqant0424
    @oqant0424 Před 8 měsíci +1

    ur consistency is an inspiration💖💖

  • @room5079
    @room5079 Před 5 měsíci +2

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

  • @suyashsinghindia
    @suyashsinghindia Před 8 měsíci +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  Před 8 měsíci

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

    • @codestorywithMIK
      @codestorywithMIK  Před 8 měsíci

      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)

  • @Nextgame999
    @Nextgame999 Před 8 dny

    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. 💥

  • @md.tamaltahasinkhan6448
    @md.tamaltahasinkhan6448 Před 8 měsíci +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: czcams.com/video/JrG4tbq6efg/video.html

  • @saikumargatla4706
    @saikumargatla4706 Před 5 měsíci

    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

  • @anshukmishra4520
    @anshukmishra4520 Před 8 měsíci +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;
    }
    };

  • @jyotiradityagautam2207
    @jyotiradityagautam2207 Před 5 měsíci

    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

  • @kamranwarsi12b22
    @kamranwarsi12b22 Před 8 měsíci +2

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

    • @ru45098
      @ru45098 Před 8 měsíci +1

      Accumulate?

    • @kamranwarsi12b22
      @kamranwarsi12b22 Před 8 měsíci

      @@ru45098 yeahh that one only

    • @codestorywithMIK
      @codestorywithMIK  Před 8 měsíci +1

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

    • @kamranwarsi12b22
      @kamranwarsi12b22 Před 8 měsíci

      @@codestorywithMIK ohkk , understood

  • @ashishdhal4614
    @ashishdhal4614 Před 8 měsíci +2

    Solved it only after seeing the sliding window tag 😢

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před 8 měsíci

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

    • @pranavchandore8591
      @pranavchandore8591 Před 8 měsíci

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

  • @bhuppidhamii
    @bhuppidhamii Před 8 měsíci +1

    Longest video I have watched

  • @user-nn5td7lw9k
    @user-nn5td7lw9k Před 7 měsíci +1

    What if , we can do both increment or decrement

  • @xiaoshen194
    @xiaoshen194 Před 8 měsíci +2

    Acha agar exactly k elements hota to kaise tweak krenge?

  • @pankajbhanwala-
    @pankajbhanwala- Před 8 měsíci +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  Před 8 měsíci +2

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

    • @pankajbhanwala-
      @pankajbhanwala- Před 8 měsíci

      explanation is great as always ❤️

  • @shivamkumarkashyap9273
    @shivamkumarkashyap9273 Před měsícem

    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;
    }
    }

  • @rajshiromani727
    @rajshiromani727 Před 2 měsíci

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

  • @ss8273
    @ss8273 Před 8 měsíci +1

    bhaiya how can i thank you???

    • @codestorywithMIK
      @codestorywithMIK  Před 8 měsíci

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

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před 8 měsíci +1

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

  • @nirmaljaat9029
    @nirmaljaat9029 Před 8 měsíci

    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

  • @Abhay14
    @Abhay14 Před 8 měsíci +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  Před 8 měsíci +1

      Can you share the test case

    • @Abhay14
      @Abhay14 Před 8 měsíci

      @@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

  • @biplavoraon9702
    @biplavoraon9702 Před 8 měsíci +1

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

    • @codestorywithMIK
      @codestorywithMIK  Před 8 měsíci +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 ❤️❤️🙏🙏😇😇

  • @sdmfslkdm
    @sdmfslkdm Před 8 měsíci +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 ;
    }
    };