It was all good until the gas stations problem arrived. From that problem, there is always something weird logic everytime in hard problems. I feel a rewatch is required.
finally, the mystery of why low and high needs to be corrected for this solution? Let's take an example, m = 3, n = 10, k = 12 If we keep low = 0, and high = 3 then mid1 = 1; low = 0 means we don't pick any element from the first array, and now the remaining elements need to be picked from the second array. mid2 = (k - mid1) = 12 - 1 = 11 ???? but there are only 10 elements in the second array Hence we can't start our search when we pick no elements from the first array. So our low must be max(k - n, 0) [no of elements at least need to pick for 1st array] Similarly, for high, we have to reduce the search space such that it can handle low K values. Note: this issue doesn't occur in the median problem because we guaranteed to split the search space in half every time.
//Bruteforce Approach class Solution { public long kthElement(int arr1[], int arr2[], int m, int n, int k) { int[] merged = mergeTwoSortedArrayIntoThirdSortedArray(arr1, m, arr2, n); return merged[k - 1]; } private int[] mergeTwoSortedArrayIntoThirdSortedArray(int[] arr1, int m, int[] arr2, int n) { int[] merged = new int[m + n]; int i = 0, j = 0, k = 0; while (i < m && j < n) { if (arr1[i]
Hey @takeUforward , there's a small mistake in the optimal solution posted on the website. It says `int low = max(0,k-m), high = min(k,n) ; ` where `m` and `n` are the length of the first and second array respectively. According to this lecture, it should be `int low = max(0,k-n), high = min(k,m) ; `
bro there is a correction if for suppose m=5 and n=6 and k=7 then if we take up all the 6 elements from arr2 then we need atleast one element to be taken from arr1. so low should be 1. so formula will be max(0,k-n) i.e 7-6=1 so low should be 1 in order to select k elements from left.
My code that's a bit different from what is explained in the video: Please do a dry run. you will get it. (Python) def kThElement(arr1, arr2, k): n1 = len(arr1) n2 = len(arr2) if n1 > n2: return kThElement(arr2,arr1,k) low = 0 high = n1 while low k: high = mid1 - 1 continue if mid2 > n2: low = mid1 + 1 continue l1 = float('-inf') r1 = float('inf') l2 = float('-inf') r2 = float('inf') if mid1 >= 1: l1 = arr1[mid1 - 1] if mid1 < n1: r1 = arr1[mid1] if mid2 >= 1: l2 = arr2[(k - mid1) - 1] if mid2 < n2: r2 = arr2[(k - mid1)] if l1
But sir, ultimately low will reach max(k-n2,0) and high will reach min(k,n1) during the execution of while loop and will reach the desired answer. Then why not leave low=0 and high=n1 as it is?? May be it takes somewhat more time but as u say it's giving wrong output.
if you pick n1 elements from a1 array (suppoes n1>k)then atlast when you find suitable configuration then in ans max(l1,l2) gives wrong answer as you picked an element which is after k so i think this might be the reson for high to be restritcted i am not sure though
Let's take an example, m = 3, n = 10, k = 12 If we keep low = 0, and high = 3 then mid1 = 1; low = 0 means we don't pick any element from the first array, and now the remaining elements need to be picked from the second array. mid2 = (k - mid1) = 12 - 1 = 11 ???? but there are only 10 elements in the second array Hence we can't start our search when we pick no elements from the first array. So our low must be max(k - n, 0) [no of elements at least need to pick for 1st array] Similarly, for high, we have to reduce the search space such that it can handle low K values. Note: this issue doesn't occur in the median problem because we guaranteed to split the search space in half every time.
//Bruteforce Approach class Solution { public long kthElement(int arr1[], int arr2[], int m, int n, int k) { int[] merged = mergeTwoSortedArrayIntoThirdSortedArray(arr1, m, arr2, n); return merged[k - 1]; } private int[] mergeTwoSortedArrayIntoThirdSortedArray(int[] arr1, int m, int[] arr2, int n) { int[] merged = new int[m + n]; int i = 0, j = 0, k = 0; while (i < m && j < n) { if (arr1[i]
@Striver Bhaiya, For K=7 , why we can't peak from arr1 all the 6 element and 1 from array2 . Just confuse with low , why it's max(k-n2,0). If any one also help, will great for me.
Watch the previous video, median of two sorted arrays, in that video he explained that we always pick elements from the smaller array, as it will reduce the time complexity. In the given example arr1[] has 6 elements and arr2[] has 5, so picked elements from arr2[].
Let's take an example, m = 3, n = 10, k = 12 If we keep low = 0, and high = 3 then mid1 = 1; low = 0 means we don't pick any element from the first array, and now the remaining elements need to be picked from the second array. mid2 = (k - mid1) = 12 - 1 = 11 ???? but there are only 10 elements in the second array Hence we can't start our search when we pick no elements from the first array. So our low must be max(k - n, 0) [no of elements at least need to pick for 1st array] Similarly, for high, we have to reduce the search space such that it can handle low K values. Note: this issue doesn't occur in the median problem because we guaranteed to split the search space in half every time.
//Bruteforce Approach class Solution { public long kthElement(int arr1[], int arr2[], int m, int n, int k) { int[] merged = mergeTwoSortedArrayIntoThirdSortedArray(arr1, m, arr2, n); return merged[k - 1]; } private int[] mergeTwoSortedArrayIntoThirdSortedArray(int[] arr1, int m, int[] arr2, int n) { int[] merged = new int[m + n]; int i = 0, j = 0, k = 0; while (i < m && j < n) { if (arr1[i]
High = min(N1, k ) It is because if we want just 2 elements in left hand side and N1 is 5 then why we need increase our search space upto 5, just simply shrink it to 2. Because we will be need only 2 elements in left hand side. Hope it helps !!!
//Bruteforce Approach class Solution { public long kthElement(int arr1[], int arr2[], int m, int n, int k) { int[] merged = mergeTwoSortedArrayIntoThirdSortedArray(arr1, m, arr2, n); return merged[k - 1]; } private int[] mergeTwoSortedArrayIntoThirdSortedArray(int[] arr1, int m, int[] arr2, int n) { int[] merged = new int[m + n]; int i = 0, j = 0, k = 0; while (i < m && j < n) { if (arr1[i]
It was all good until the gas stations problem arrived. From that problem, there is always something weird logic everytime in hard problems. I feel a rewatch is required.
very true brother
the whole confidence goes off
glad someone said
Soo true
Facts, everything was going good till that gas station problem 😭
But the gas station problem requires premium on leetcode right? 👀
did it by my own after learning median of two sorted arrays concept from you! thank you
which one ?
I AM IMPROVING MYSELF IN PROBLEM SOLVING DAY BY DAY BECAUSE OF YOU.
I AM SLAVE TO YOUR PLAYLIST
what the actual f lmao
finally, the mystery of why low and high needs to be corrected for this solution?
Let's take an example, m = 3, n = 10, k = 12
If we keep low = 0, and high = 3
then mid1 = 1;
low = 0 means we don't pick any element from the first array, and now the remaining elements need to be picked from the second array.
mid2 = (k - mid1) = 12 - 1 = 11 ???? but there are only 10 elements in the second array
Hence we can't start our search when we pick no elements from the first array.
So our low must be max(k - n, 0) [no of elements at least need to pick for 1st array]
Similarly, for high, we have to reduce the search space such that it can handle low K values.
Note: this issue doesn't occur in the median problem because we guaranteed to split the search space in half every time.
bro when mid1=1 then you pick one element from first array correction rest are correct
can you furthur explain why it does not happens in median.?
//Bruteforce Approach
class Solution {
public long kthElement(int arr1[], int arr2[], int m, int n, int k) {
int[] merged = mergeTwoSortedArrayIntoThirdSortedArray(arr1, m, arr2, n);
return merged[k - 1];
}
private int[] mergeTwoSortedArrayIntoThirdSortedArray(int[] arr1, int m, int[] arr2, int n) {
int[] merged = new int[m + n];
int i = 0, j = 0, k = 0;
while (i < m && j < n) {
if (arr1[i]
In median it will work because we never ask to pick more than the largest array have, because of the formula (n1+n2+1)//2
Hey @takeUforward , there's a small mistake in the optimal solution posted on the website. It says `int low = max(0,k-m), high = min(k,n) ; ` where `m` and `n` are the length of the first and second array respectively. According to this lecture, it should be `int low = max(0,k-n), high = min(k,m) ; `
Just search for this question's solution and here comes the video just 4 hours ago!! Thanks for your help!
Understood! Super fantastic explanation as always, thank you very very much for your effort!!
UNDERSTOOD......Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
I think some questions are made to skip and this is also one of those inlcuding medain of sorted arrays
Awesome 😎👍🏻 11:41
Bhaiya which topic will come next ?.....what are upcoming plans
bro there is a correction if for suppose m=5 and n=6 and k=7 then if we take up all the 6 elements from arr2 then we need atleast one element to be taken from arr1. so low should be 1.
so formula will be max(0,k-n)
i.e
7-6=1
so low should be 1 in order to select k elements from left.
Thanks stiver, love you. See you at google soon.
To be precise low=max(0, k-sizeoflargerarray)
we always consider n2 as larger array bro
Great explanation
My code that's a bit different from what is explained in the video:
Please do a dry run. you will get it.
(Python)
def kThElement(arr1, arr2, k):
n1 = len(arr1)
n2 = len(arr2)
if n1 > n2:
return kThElement(arr2,arr1,k)
low = 0
high = n1
while low k:
high = mid1 - 1
continue
if mid2 > n2:
low = mid1 + 1
continue
l1 = float('-inf')
r1 = float('inf')
l2 = float('-inf')
r2 = float('inf')
if mid1 >= 1:
l1 = arr1[mid1 - 1]
if mid1 < n1:
r1 = arr1[mid1]
if mid2 >= 1:
l2 = arr2[(k - mid1) - 1]
if mid2 < n2:
r2 = arr2[(k - mid1)]
if l1
it's the same
UnderStood
understood😍
Thank you Bhaiya
thansks
Forever strivers
understood bhaiya
Understood, thank you.
But sir, ultimately low will reach max(k-n2,0) and high will reach min(k,n1) during the execution of while loop and will reach the desired answer. Then why not leave low=0 and high=n1 as it is?? May be it takes somewhat more time but as u say it's giving wrong output.
if you pick n1 elements from a1 array (suppoes n1>k)then atlast when you find suitable configuration then in ans max(l1,l2) gives wrong answer as you picked an element which is after k so i think this might be the reson for high to be restritcted i am not sure though
Let's take an example, m = 3, n = 10, k = 12
If we keep low = 0, and high = 3
then mid1 = 1;
low = 0 means we don't pick any element from the first array, and now the remaining elements need to be picked from the second array.
mid2 = (k - mid1) = 12 - 1 = 11 ???? but there are only 10 elements in the second array
Hence we can't start our search when we pick no elements from the first array.
So our low must be max(k - n, 0) [no of elements at least need to pick for 1st array]
Similarly, for high, we have to reduce the search space such that it can handle low K values.
Note: this issue doesn't occur in the median problem because we guaranteed to split the search space in half every time.
@@FusionArcs understood, thanks😀
//Bruteforce Approach
class Solution {
public long kthElement(int arr1[], int arr2[], int m, int n, int k) {
int[] merged = mergeTwoSortedArrayIntoThirdSortedArray(arr1, m, arr2, n);
return merged[k - 1];
}
private int[] mergeTwoSortedArrayIntoThirdSortedArray(int[] arr1, int m, int[] arr2, int n) {
int[] merged = new int[m + n];
int i = 0, j = 0, k = 0;
while (i < m && j < n) {
if (arr1[i]
read solution in the sheet for clear explanation.
Understood !! 😍😍
Understood!
Understood✅🔥🔥
UNDERSTOOD
please post the link to the prerequisite video.
Thank you 😊❤
understood
Understood
easy 120 points :)
Completed
29th May 2024
Done
why these low and high conditions were not in median problem?
why was the low and high check was not needed in the median problem?
❤
Where are the notes?
Why marked as done, if it didn't work?
8:25
🎉🎉❤
@Striver Bhaiya, For K=7 , why we can't peak from arr1 all the 6 element and 1 from array2 . Just confuse with low , why it's max(k-n2,0). If any one also help, will great for me.
since, low=max(k-n2,0) so it indicates we have to select atleast these many elements from arr1.
@@vivekverma4012 that I got it, my question why not to pick from arr1 first rather taking from arr2 first
Watch the previous video, median of two sorted arrays, in that video he explained that we always pick elements from the smaller array, as it will reduce the time complexity.
In the given example arr1[] has 6 elements and arr2[] has 5, so picked elements from arr2[].
Let's take an example, m = 3, n = 10, k = 12
If we keep low = 0, and high = 3
then mid1 = 1;
low = 0 means we don't pick any element from the first array, and now the remaining elements need to be picked from the second array.
mid2 = (k - mid1) = 12 - 1 = 11 ???? but there are only 10 elements in the second array
Hence we can't start our search when we pick no elements from the first array.
So our low must be max(k - n, 0) [no of elements at least need to pick for 1st array]
Similarly, for high, we have to reduce the search space such that it can handle low K values.
Note: this issue doesn't occur in the median problem because we guaranteed to split the search space in half every time.
//Bruteforce Approach
class Solution {
public long kthElement(int arr1[], int arr2[], int m, int n, int k) {
int[] merged = mergeTwoSortedArrayIntoThirdSortedArray(arr1, m, arr2, n);
return merged[k - 1];
}
private int[] mergeTwoSortedArrayIntoThirdSortedArray(int[] arr1, int m, int[] arr2, int n) {
int[] merged = new int[m + n];
int i = 0, j = 0, k = 0;
while (i < m && j < n) {
if (arr1[i]
showing TLE on code 360 please tell how to evaluate.
Why high=max(k,n1)? I am unable to understand.
High = min(N1, k )
It is because if we want just 2 elements in left hand side and N1 is 5 then why we need increase our search space upto 5, just simply shrink it to 2. Because we will be need only 2 elements in left hand side.
Hope it helps !!!
//Bruteforce Approach
class Solution {
public long kthElement(int arr1[], int arr2[], int m, int n, int k) {
int[] merged = mergeTwoSortedArrayIntoThirdSortedArray(arr1, m, arr2, n);
return merged[k - 1];
}
private int[] mergeTwoSortedArrayIntoThirdSortedArray(int[] arr1, int m, int[] arr2, int n) {
int[] merged = new int[m + n];
int i = 0, j = 0, k = 0;
while (i < m && j < n) {
if (arr1[i]
can anyone tell why does this code not pass on gfg I have been trying to do it for 2hrs now and am unable to find the error
if(n > m) return this.kthElement(B , A , m , n , k)
let i = Math.max( 0 , k-m) , j = Math.min(k , n)
while(i = 0) l1 = A[mid1-1]
if( mid2 < m) r2 = B[mid2]
if(mid2 - 1 >= 0 ) l2 = B[mid2 - 1]
if(l1
class Solution {
public long kthElement( int a1[], int a2[], int n1, int n2, int k) {
if(n1 > n2) return kthElement(a2, a1, n2, n1, k);
int low = Math.max(0, k - n2);
int high = Math.min(k, n1);
while(low 0) l1 = a1[mid1 - 1];
if(mid2 > 0) l2 = a2[mid2 - 1];
if(l2 > r1) low = mid1 + 1;
else if(l1
3rd comment
love u striver...
us
kachwa bhai har jagah aa jata h
striver bhai kachwa bhai ko rok lo
Understood!
Understood
understood
Understood!
Understood
understood
Understood
understood
Understood
understood
Understood
understood
understood
Understood
Brother can you explain why the changes for low and high stuffs were used here and not in the median qn, am a bit confused in that ...
Understood
understood
Understood
understood
understood