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

  Рет қаралды 12,906

codestorywithMIK

codestorywithMIK

Күн бұрын

Whatsapp Community Link : www.whatsapp.c...
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/MAZ...
Leetcode Link : leetcode.com/p...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My GitHub Repo for interview preparation : github.com/MAZ...
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

Пікірлер: 106
@newglobal2056
@newglobal2056 Жыл бұрын
College ke computer lab me apka lecture dekh raha hu, ha teacher se permission lia hai maja agaya bhaiya ❤❤❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Made my day ❤️❤️❤️😄😇🙏
@wearevacationuncoverers
@wearevacationuncoverers Жыл бұрын
Damn 😅. Our lab teachers won't even let us open KZbin. Else I would have done the same 🥲
@newglobal2056
@newglobal2056 Жыл бұрын
@@codestorywithMIK ❤️❤️
@newglobal2056
@newglobal2056 Жыл бұрын
@@wearevacationuncoverers 😄
@DevOpskagyaan
@DevOpskagyaan Жыл бұрын
Nicee 😄
@YashSharma-fs3mn
@YashSharma-fs3mn Жыл бұрын
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 Жыл бұрын
this was indeed a good problem, definately on the medium hard side, i am happy that i learned something new today
@oqant0424
@oqant0424 Жыл бұрын
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 Жыл бұрын
Most welcome 😊
@Zomb-zj4ip
@Zomb-zj4ip 5 ай бұрын
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!!!
@codestorywithMIK
@codestorywithMIK 5 ай бұрын
You're so welcome! ❤️🙏
@raynwani137
@raynwani137 3 ай бұрын
Bhai bohot bohot shukriya ! Bohot pyaar se samjaya!
@bash-ian
@bash-ian 4 ай бұрын
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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
bhai learned lot of things from this video. thanks.
@YashMalav-kh1ov
@YashMalav-kh1ov 8 ай бұрын
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 ❤❤
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
❤️❤️🙏🙏
@study-yd6es
@study-yd6es 2 ай бұрын
keep uploading these type of lecture sir
@unknown47896
@unknown47896 5 ай бұрын
hatsoff to ur fist apprach bro..
@OIAOa
@OIAOa Жыл бұрын
Your doing a lot of hardwork for us ....❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much for watching 🙏🙏🙏
@akshayrahangdale8511
@akshayrahangdale8511 9 ай бұрын
Very Nicely Explained.
@tanishasethi7363
@tanishasethi7363 7 ай бұрын
Loved the explanation 🤌✨️
@anshukmishra4520
@anshukmishra4520 Жыл бұрын
I love watching your videos. Doing great bhaiya.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 😀
@KshitijPratyush
@KshitijPratyush 12 күн бұрын
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 12 күн бұрын
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 11 күн бұрын
@@codestorywithMIK Thank you so much. I will watch your videos and start practicing.
@Abc-rz4ps
@Abc-rz4ps 4 ай бұрын
Ur voice is cute Bhaiya 😊
@Anonymoussssss-l6r
@Anonymoussssss-l6r Жыл бұрын
very nice explanation
@anuppatankar4294
@anuppatankar4294 Жыл бұрын
Great Explanation 🔥
@satwikghildiyal402
@satwikghildiyal402 Жыл бұрын
Incredible content
@infinitygaming7192
@infinitygaming7192 Жыл бұрын
very nice explnation sir
@oqant0424
@oqant0424 Жыл бұрын
Thank u so much...........learnt a lot 💖💖
@rolls423
@rolls423 Жыл бұрын
thank u so much
@sauravchandra10
@sauravchandra10 Жыл бұрын
Amazing explanation, I like these in-depth videos, teaches me a lot.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Glad it was helpful! 🙏😇
@rohitshah8904
@rohitshah8904 Жыл бұрын
Very nicely explained ❤
@ugcwithaddi
@ugcwithaddi 11 ай бұрын
👌🏻👌🏻👌🏻That’s pure Art
@sukomal07
@sukomal07 Жыл бұрын
best of best , keep doing ❤
@suyashsinghindia
@suyashsinghindia Жыл бұрын
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 Жыл бұрын
Again THE best 😊
@oqant0424
@oqant0424 Жыл бұрын
ur consistency is an inspiration💖💖
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Means a lot 😇🙏
@codestorywithMIK
@codestorywithMIK Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
Amazing ❤❤❤❤
@reshusingh3558
@reshusingh3558 Жыл бұрын
Thankyou sir
@room5079
@room5079 9 ай бұрын
if we have array like [1,1,1,4] and k=3 then we can have 2 as max frequency instead of 4.?????
@baibhav1559
@baibhav1559 3 ай бұрын
yes exactly,
@simnidhnidh9122
@simnidhnidh9122 2 ай бұрын
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 Жыл бұрын
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: kzbin.info/www/bejne/gKOqZaeYppuYnMk
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Same bro
@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 Жыл бұрын
Solved it only after seeing the sliding window tag 😢
@kamranwarsi12b22
@kamranwarsi12b22 Жыл бұрын
instead of prefix sum can't we use inbuilt stl function for computing sum ?? Will it be good or not ?
@ru45098
@ru45098 Жыл бұрын
Accumulate?
@kamranwarsi12b22
@kamranwarsi12b22 Жыл бұрын
@@ru45098 yeahh that one only
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Actually accumulate will internally take O(n) time. That’s why prefix sum array is considered.
@kamranwarsi12b22
@kamranwarsi12b22 Жыл бұрын
@@codestorywithMIK ohkk , understood
@pankajbhanwala-
@pankajbhanwala- Жыл бұрын
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 Жыл бұрын
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 11 ай бұрын
Longest video I have watched
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you for watching 🙏😇❤️
@RahulKumar-x6m2k
@RahulKumar-x6m2k 11 ай бұрын
What if , we can do both increment or decrement
@KumarMuravath-YearBTechChemica
@KumarMuravath-YearBTechChemica 11 ай бұрын
follow up question aaj ka weekly contest 376 D
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Qn-4 kzbin.info/www/bejne/f5LdXmuQhr6IrNUsi=4qCFdzYompcrUyhy
@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 Жыл бұрын
Acha agar exactly k elements hota to kaise tweak krenge?
@xiaoshen194
@xiaoshen194 Жыл бұрын
nvm we just change the condition from
@wearevacationuncoverers
@wearevacationuncoverers Жыл бұрын
@@xiaoshen194 yes. I think in that case == karke hojaega
@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 Жыл бұрын
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 Жыл бұрын
The binary solution was explained really well. However, the sliding window explanation was non intuitive.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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 ; } };
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Always a good thing to try multiple approaches
World’s strongest WOMAN vs regular GIRLS
00:56
A4
Рет қаралды 51 МЛН
СКОЛЬКО ПАЛЬЦЕВ ТУТ?
00:16
Masomka
Рет қаралды 3,3 МЛН
Симбу закрыли дома?! 🔒 #симба #симбочка #арти
00:41
Симбочка Пимпочка
Рет қаралды 4,4 МЛН
Coding Unbreakable Encryption in C | One-Time Pad
17:42
HirschDaniel
Рет қаралды 4,3 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Beginners Should Think Differently When Writing Golang
11:35
Anthony GG
Рет қаралды 123 М.
Arpit Bhayani talks about real engineering for 1 hour straight
1:16:23
World’s strongest WOMAN vs regular GIRLS
00:56
A4
Рет қаралды 51 МЛН