Please watch our new video on the same topic: kzbin.info/www/bejne/emLSdaqNeNZoZsk
@sonalidutta8252 жыл бұрын
Those who didn't understood this part -----> low = max(0,k-m), high = min(k,n); Let's understand with the help of the example used in 22:30 . Here n = 4 and m=6 and k=7. Since k>m , so we can't take 0 elements as the lowest no. of elements picked from array1 . It should be (k-m) i.e. 7-6=1 and the high is obvious min(k,n) i.e min(7,4) i.e 4 (4 elements can be taken at max from array 1) My Code: #include int median(vector& nums1, vector& nums2,int k){ int n=nums1.size(),m=nums2.size(),x=k,sum=0; int l = max(0,k-m) ,r = min(k,n) ,l2,r2,l1,r1; int cut1 = (l+r)/2,cut2 = x-cut1; while(lr2){ r = cut1-1; } else if(l2>r1){ l = cut1+1; } else{ return max(l1,l2); } } return 0; } int ninjaAndLadoos(vector &nums1, vector &nums2, int n, int m, int k) { int medi; if(n
@kingmaker9082 Жыл бұрын
Thanks for saving time 🙏🏻
@shubhampatel_2745 Жыл бұрын
Really Thank you from bottom of my heart.
@crictYaari Жыл бұрын
Bro but what if k=15 and m =10, n= 20 then low comes out low = 5, and high = 15 now if we doing operations on big 20 size array then why we cant choose any 0 to 15 or 1 to 16 ..... May be i am somwhere confused , which array to consider , please clear it
@anuradha7861 Жыл бұрын
Thanks dear for explaining this 🙏
@vijaykaran3848 Жыл бұрын
Thank you very much
@jaineelmamtora67603 жыл бұрын
There can't be any more simplified explanation video on KZbin other than this! Thank you for making this video!
@rishabhkumar81153 жыл бұрын
A great thanks to you striver, I have learned this problem by heart and have also capable of writing 4-page notes of this by myself. You are such a great teacher. You are changing the way of education from your hard work. Keep doing this.
@lokeshdulani60353 жыл бұрын
I was seriously struggling to understand this from GFG's editorial thanks for this.
@gauravshukla52033 жыл бұрын
how do we determine that our last element will always be kth element?
@princemakadiya94433 жыл бұрын
@@gauravshukla5203 all time we are taking total k elements from both array for example if k=5, 2 from 1st and 3 from 2nd array, so every time there will be k elements total from both array, at once, l1,l2,r1,r2 condition may hold true so kth element will be max of last two
@devprakash53203 жыл бұрын
settting the right range of low and high initially is one of the most important steps
@devprakash53203 жыл бұрын
forgot to write 'Understood" 😊😊😊
@yushdecides3 жыл бұрын
I didn't understand that part, can you plz explain it to me?
@exodus59483 жыл бұрын
I guess he meant in Binary search the main thing is to get the search space lower bound and upper bound half of the work is done when you have your search space.
@yushdecides3 жыл бұрын
@@exodus5948 Are or unki values vo kyu li..ye puchra tha
@exodus59483 жыл бұрын
@@yushdecides Sorry I misunderstood
@willturner34403 жыл бұрын
Before your explanation I made my logic..bcoz this was same as previous. Thanks for such a great explanation ..
@Rajat_maurya2 жыл бұрын
were you able to write the code of edge case?
@inclinedscorpio2 жыл бұрын
@@Rajat_maurya obviously it's clear once test case starts breaking.
@ankitranjan882 жыл бұрын
Did it by Own from using Concept of (Median of two sorted ) ... Thanxx Bhaiya for this Explanation
@Sakshi-yq4jm5 ай бұрын
you just need to find kth smallest number all in all. It might seem complicated but it basically is to ensure that th elements are smaller than leftover not chosen elements and we dont compare it with its own array because that particular array is sorted so just need to keep a check of the array for a particular element.
@lavishgarg50903 жыл бұрын
Hi bhaiya,as you have mentioned in the SDE sheet,bits are rarely asked in interviews,so it will be a great help for us,if you can cover that topic at last and continue with Stack and Queues,ie the next topic
@superheroherohero2 жыл бұрын
First thank you very much! It is an awesome solution. But I have a question regarding Time Complexity, I believe it should be O(log(min(n, m, k))) cause the case of k < m and also k < n. This binary search is on the range of number of elements taken from smaller array.
@MOHDSALMAN-sj2zu2 жыл бұрын
Yes, you are correct. It is O(log(min(n, m, k))).
@_-69123 жыл бұрын
After median of sorted arrays this was really easy to understand!
@sauravchandra10 Жыл бұрын
Other than the edge cases, I was able to code this one on my own. Most optimised approach, maza agya
@kaichang8186Ай бұрын
understood, thanks for the perfect explanation
@nagame859 Жыл бұрын
Understood 👍 The way you explained edge cases exemplifies your proficiency. Great work as always..
@VenuGopal-hg1hm3 жыл бұрын
You already explaining all the things in crystal clear manner so why people waste money and join other courses
@geck12043 жыл бұрын
Had this question come up on an interview today. Nailed the pointer approach but fumbled on the binary search approach... Jeeze that required some out of the box thinking
@geck12043 жыл бұрын
Great explanation btw!
@ayeshaqaisar16513 жыл бұрын
@@geck1204 Hey, Which company's interview was this asked in?
@geck12043 жыл бұрын
@@ayeshaqaisar1651 Cruise
@ayeshaqaisar16513 жыл бұрын
@@geck1204 Thanks
@akshatsrivastava90153 жыл бұрын
Clearly explained all the boundary cases. Keep up the good work!
@ranaaditya7960 Жыл бұрын
Usually (never) I don't comment on any YT videos but this time I want to say THANK YOU STRIVER 🥺
@Sakshi011882 жыл бұрын
class Solution { public long kthElement( int arr1[], int arr2[], int n, int m, int k) { if(n>m){ return kthElement(arr2,arr1,m,n,k); } int low=Math.max(0,k-m), high=Math.min(k,n); while(low>1; int cut2=k-cut1; int l1=cut1==0?Integer.MIN_VALUE:arr1[cut1-1]; int l2=cut1==0?Integer.MIN_VALUE:arr1[cut1-1]; int r1=cut1==n?Integer.MAX_VALUE:arr1[cut1]; int r2=cut1==m?Integer.MAX_VALUE:arr1[cut1]; if(l1
@jacksparrow13163 жыл бұрын
Thank you sir for giving your precious time from your busy schedule....♥️♥️
@takeUforward3 жыл бұрын
It's my pleasure
@yourGuy6752 жыл бұрын
i think we can do these when validating the array. max(l1,l2) < min(r1,r2) then it is our desired array
@ITKOUSIKV3 жыл бұрын
You are gem of ther person for students
@INC6272 жыл бұрын
How can k be max(l1,l2) what if he gives k=9 for n1+n2=11
@rahulpothula19022 жыл бұрын
can anyone find out the mistake, my code (in c++) seems to be the same as the logic: class Solution{ public: int kthElement(int arr1[], int arr2[], int n, int m, int k) { // if(m < n) // return kthElement(arr2, arr1, m, n, k); int lo = 0, hi = n; if(k > n - 1) lo = k - m; else if(k < n / 2) hi = k; while(lo
@GauravJain-zo8gt5 ай бұрын
jai jinendra sir
@monilcharola68732 жыл бұрын
Hi Striver and Everyone in the Comment Section ! I attempted the problem with this same method but in the function after finding l1,l2,r1 and r2. I added below given conditions if(l1==INT_MIN) // If not selecting any element from the first array, then return answer from the second array return arr2[k-1]; if(l2==INT_MIN) // Similarly, if we are not selecting any element from the second array we return the answer from 1st array return arr1[k-1]; if(r1==INT_MAX || r2==INT_MAX) // If one of the array is completely considered in the answer we have reached the solution thus return max(l1,l2) return max(l1,l2); But due to some reason, the code is missing on one of the TC on GFG. Can anyone please explain to me where I am getting wrong. Thanks in Advance.
@PhoenixRisingFromAshes4712 жыл бұрын
code given in TUF website is little wrong, the the line int low = max(0,k-m), high = min(k,n); given in website has m,n interchanged just do it int low = max(0,k-n), high = min(k,m); and remove all of your edge caes and it will work like charm Thank me later
@indrajitdas9553 Жыл бұрын
@@PhoenixRisingFromAshes471 I tried that but still get error on 7 11 15 1 10 10 25 40 54 79 15 24 27 32 33 39 48 68 82 88 90 this
@Steve-kv5we Жыл бұрын
@@indrajitdas9553, Can you share your complete code here, so that I can figure out where the problem lies if possible from my side?
@Steve-kv5we Жыл бұрын
@Monil Charola, the conditions which you have introduced are wrong and that is why it is giving the wrong answer. Let us take an example test case:- arr1-> [6] arr2-> [1,2,3,4,5,7,8,9,10,11] k=8 Your code will give the asnwer=arr2[7] which is 9 but the correct answer is 8. The reason which I can deduce from your conditions is that: -> If we are not selecting any element from the first or second array, then you are neglecting that whole array and only considering the other array but that is not the case. Even if we are not selecting any element from an array, then also that array will contribute to our answer in the above case "6" from the first array will come at the 6th position of our final sorted array but as per your condition you will return arr2[k-1] which will be our 7th element of the second array and not the final sorted array because you have neglected the whole "arr1". This is the reason your code is giving the wrong answer. Hope you understood, if you find any difficulty understanding the reason, you can ask me again🙌.
@PremKumar-rd1uv3 жыл бұрын
Thanks for the clear explanation brother. Understood well. Daily I'm waiting for ur videos.
@suryanshm0002 жыл бұрын
I appreciate your observation of second edge case 👌👌 clearly explained !!
@cse-b-132ashishupadhyaya53 жыл бұрын
23:10 commenting to remember edge case.... bdw nice work bro
@abhijeetpatnaik54943 жыл бұрын
Can't thank enough for this beautiful solution
@tanishqmehta82722 жыл бұрын
can't be a more noble deed than to clear a student's concepts❤❤
@LegitGamer23453 жыл бұрын
thanks a lot i have been stuck on this for way too long
@vaishnavi97552 жыл бұрын
Hi, I was running the below test case arr1 = {1, 5,9} arr2 = {4,7,11,18,19} k = 7 The code fails for this test case. Answer should be 18. Upon trying to understand I think the below logic is causing the test case to fail. int low = max(0,k-m), high = min(k,n); Can anyone explain if I am doing something wrong here ?
@vaishali18432 жыл бұрын
I had the similar question. My test case was failing at a similar test case.
@amitchaudhary77452 жыл бұрын
use this code public long kthElement( int arr1[], int arr2[], int n, int m, int k) { if(arr2.length
@Steve-kv5we Жыл бұрын
@@vaishali1843 Can anyone of you share the complete code so that I can figure out exactly where the problem lies?
@shivam92012 жыл бұрын
can someone explain the reason of taking low = max(0, k-m) and high = min(n, k)??
@varunsharma83503 жыл бұрын
Did you miss a case where K equals number of elements in both the arrays combined? arr1 = {12, 14} arr2 = {1,2,3,4,5} k = 7
@takeUforward3 жыл бұрын
Nah, it works on all, you can try with code in description.
@jayadubey_223 жыл бұрын
Great explanation understood very well this complex binary search problem😅 thank you very much bhaiyaa
@arijit9862 Жыл бұрын
Hello Striver! I'm Smeet. This code will fail in test cases where k - m > m. Why? Because the low will be initialized greater than high, resulting in skiping the execution of while loop. Hence, the correct initialization should be : int low = max(0 , min(k - m , m)); This will take care of the test cases. And Thank you so much for this valuable content.
@arijit9862 Жыл бұрын
@satvikshrivastava5840 You might be having some bugs, Kindly refer to this code : #include using namespace std; int ninjaAndLadoos(vector &row1, vector &row2, int m, int n, int k) { if(m > n){ return ninjaAndLadoos(row2 , row1 , n , m , k); } int low = max(0 , min(k - m , m)); int high = min(m , k); int cut1; while(low
@hemantranjan22972 жыл бұрын
20:36 Leonardo DiCaprio -> holding bear -> pointing on screen.
@팜팜팜-g6p2 жыл бұрын
I finally understood!!!! Thanks a lot!!💛
@ritikjalal1173 жыл бұрын
great explanation bhaiya understood
@biswajitmahalik9979 Жыл бұрын
so well explained!!!!!
@samarthjain52952 жыл бұрын
It is not working when k=10 for your two arrays. Actually here mid value is greater than size of smaller array. That gives run time error so to avoid that we will work on larger array rather than working on smaller array
@sujayshanbhag20552 жыл бұрын
DOUBT!!!! Isnt the binary search done on the smaller array so that not more than needed elements are taken (in the median problem half the total elements, and here k elements) but if we make sure the high is not more than k, it shouldnt matter what array we do binary search on, right? i tried accordingly, and my solution got accepted(GFG). Is there any other reason why binary search must be done on the smaller array? please reply if know the answer or maybe atleast have the same doubt guys..
@aasifali91392 жыл бұрын
i have understood the code. but there is one thing: this code gives wrong ans when submitted for all test cases on gfg. Also i dont know why but it also gives TLE in some cases. I thought may be i would have done something wrong but then i went to the take youforward site and pasted the code for this question from there but it passes only 1 test case. pllzz relply if someone has the solution............
@Sakshi011882 жыл бұрын
same for java class Solution { public long kthElement( int arr1[], int arr2[], int n, int m, int k) { if(n>m){ return kthElement(arr2,arr1,m,n,k); } int low=Math.max(0,k-m), high=Math.min(k,n); while(low>1; int cut2=k-cut1; int l1=cut1==0?Integer.MIN_VALUE:arr1[cut1-1]; int l2=cut1==0?Integer.MIN_VALUE:arr1[cut1-1]; int r1=cut1==n?Integer.MAX_VALUE:arr1[cut1]; int r2=cut1==m?Integer.MAX_VALUE:arr1[cut1]; if(l1
@youngshahrukhkhan81793 жыл бұрын
Understood....Awesome Explanation......Especially handling edge cases was so fun
@ayushkaushik96373 жыл бұрын
Great explanation great mind
@amitchaudhary77452 жыл бұрын
if somebody's solution is failing for some test cases on gfg use this code intuition is same public long kthElement( int arr1[], int arr2[], int n, int m, int k) { if(arr2.length
@yadneshkhode30912 жыл бұрын
int mid2 = k - mid1; how can we be sure k > mid1 ??
@Steve-kv5we Жыл бұрын
@@yadneshkhode3091 From the conditions, we can conclude that "low" cannot be "less than 0" and "high" cannot be "greater than k". So, our cur1 will be at max "k/2" in the starting and as we move further, we are updating our low and high to "cur1+1" and "cur1-1" which implies our low and high will always lie "between 0 and k". Since cur1 is calculated as (low+high)/2, we can say it will never become "greater than k". Hope you understood🙌.
@anushkamishra64113 жыл бұрын
Is it okay to go for a optimise solution for any question even if you're not sure about it's brute force approach? I mean is it necessary to know? And the other thing I'm stuck with is implementing the approach/solution into code. Also, whenever I read a question I know what to implement at this condition but I cannot write the solution in order or you can say in structurzied manner
@mickyman7533 жыл бұрын
if you will not say the brute force , the problem will be finished soon and interviewer will be ready with another extra problem for you ,so time management with brute force is a important step
@anushkamishra64113 жыл бұрын
@@mickyman753 lol your way of approaching the situation is quite intresting... anyway, thanks I'll keep that in mind
@debaratighatak22113 жыл бұрын
Thank you bhaiya for the amazing ,totally understood the concept :D
@balajiarumugam18762 жыл бұрын
++CFBR. Great work bhai !
@arfat25973 жыл бұрын
Great explanation. Thank You!
@SuperWhatusername2 жыл бұрын
Thank you Stryver
@ashwinshetgaonkar63293 жыл бұрын
thanks for this authentic explaination .
@sahajpareek63522 жыл бұрын
Bruh how do I know that the last element in the left half is k?
@koelsinha3800 Жыл бұрын
Hi! How do we solve for 4th element of m sorted arrays>?
@iamsks73 жыл бұрын
understood. very well explained
@gauravbhardwaj3802 жыл бұрын
Can anyone pl explain me how we have chosen low and high initially? Like low,high = max(0,k-m),min(k,n)
@meghadandapat2132 жыл бұрын
Go to 21:42 for the expalination of this edge case
@sherryfan1612 жыл бұрын
great video, i finally understand it!
@arnabpersonal67293 жыл бұрын
just excellent explanation
@cinime Жыл бұрын
Understood! Super wonderful explanation as always, thank you very much!!
@littlecuties3384 Жыл бұрын
U r doing a great job .but my bad 😔.I dint understand
@manavmalhotra85132 жыл бұрын
if (counter != k) { if (p1 != m - 1) answer = array1[k - counter]; else answer = array2[k - counter]; } can anyone please explain this block for naive sol.?
@mukuldaftary92302 жыл бұрын
Superb Solution !!
@385_ayushagarwal83 жыл бұрын
great explanation as always !!!
@gauravshukla52033 жыл бұрын
how do we determine that our last element will always be kth element
@vrushin10533 жыл бұрын
wonderful explanation! thanks
@resetengineering Жыл бұрын
I haven't understood the point made at 23:00. How is the low 1?
@amitpurohit88162 жыл бұрын
Best explanation!!
@rohitbajaj77332 жыл бұрын
Why it's not working for some cases if we set high=n only?
@himanshubarak49443 жыл бұрын
very well explained !!
@growwithriu2 жыл бұрын
Edge cases: 21:41
@quanta83823 жыл бұрын
Thanks sir! This is a beautiful explanation...the method itself is ingenious. Because of geniuses like you plebs like me will hopefully get a job :D
@kunalpatidar28493 жыл бұрын
Understood completely 👍
@Steve-kv5we Жыл бұрын
Understood💯💯
@vasujain19703 жыл бұрын
Love the amazing content...Do keep it up! It's helping us out in ways you couldn't imagine!!
@takeUforward3 жыл бұрын
Thank you! Will do!
@sreeharia23562 жыл бұрын
Can somebody explain why low = max(0,k-m) ?
@preethikadambala25952 жыл бұрын
in the video he has said 2 edge cases, watch it, for that edge cases to be handled, minimum elements from array1 cannot be zero if array2 does not have k elements . So, low cannot be always zero
@akshaybhadange24923 жыл бұрын
Thanks my friend really help !!!
@ishanmay83 жыл бұрын
great approach
@geekySRM3 жыл бұрын
Understood bro!
@lakshmiprasanna7058 Жыл бұрын
Understood 💯💯💯
@niketgupta38842 жыл бұрын
hii striver @take U forward i think maybe your end cases will fail in that condition if arr1=[1,2,3,4,5] and arr2=[6,7,8,9]... plzz check this
@niketgupta38842 жыл бұрын
@takeUforward your condition low=max(0,k-m) high=min(k,n)
@rohitraina50593 жыл бұрын
Understood. Great video🔥🔥
@rkalyankumar Жыл бұрын
Just wow!
@SatyamKumar-bw4vi2 жыл бұрын
Hare Krishna! understood
@AkashKumar-lr6hc2 жыл бұрын
Why we call function on smaller array ?
@DIVYANSHMISHRA083 жыл бұрын
hello sir, in SDE sheet there are no questions like book allocation problem, painters partition which are solved using binary search in answers. Are they not asked much in interviews? (sorry if you have already answered this question somewhere and I missed that)
@takeUforward3 жыл бұрын
I have covered tougher problems on the same topics, so that when these problems are asked, it becomes easier. However the painter’s partition is a good one, will add it.
@mukulrana16163 жыл бұрын
Greatly helpful
@jsuryakt3 жыл бұрын
Love the explanation ❤️
@hit1599 Жыл бұрын
Thanks bro!
@sakshamsrivastava62803 жыл бұрын
understood, Thankyou
@AmitKumar-je9qo3 жыл бұрын
Bhaiya aptitude bhi padhna hoga kya ya sheet me jitna hai utna se ho jayega??
@mrb79313 жыл бұрын
too tough, I am afraid i could crack interview or not
@maruthiteja2586 Жыл бұрын
Understood 👌
@aritraganguly39572 жыл бұрын
can't understand the logic behind k-m 25:10
@azraazra82733 жыл бұрын
Good bro,continue this
@nuamaaniqbal63732 жыл бұрын
Thanks a lot!
@decostarkumar25623 жыл бұрын
I am getting ArrayIndexOutOfBoundException 😞🤔 Can anyone help 😭
@TSMJonathan-po3hj2 жыл бұрын
broo please change java code on your Website .the solution in u r site was wrong please update