Re: [algogeeks] Re: kth largest element in two sorted arrays
Hello Venkat One scenario that is troubling me is what if p2 is not a valid position in l2? findNth(start,end) p1 = (start + end)/2 p2 = n-p1 if l1[p1] < l2[p2]: if l1[p1 + 1] > l2[p2]: return l2[p2] else: return findNth(p1+1, end) else: if l2[p2 + 1] > l1[p1]: return l1[p1] else: return findNth(start,p1-1) Best Regards Ashish Goel "Think positive and find fuel in failure" +919985813081 +919966006652 On Wed, Feb 1, 2012 at 11:41 AM, Venkat wrote: > Hey Ashish check this link > http://stackoverflow.com/questions/8999610/median-of-lists > > Thanks and Regards, > Venkat Gottipati > > On Jan 31, 10:14 am, Ashish Goel wrote: > > i think this can be done much faster similar to findling median of two > > sorted arrays by proceeding with comparing medians of two arrays and then > > reducing the data set to approx 3/4th of 2n. I am looking for that algo > if > > osmeone have. > > > > Best Regards > > Ashish Goel > > "Think positive and find fuel in failure" > > +919985813081 > > +919966006652 > > > > > > > > > > > > > > > > On Tue, Jan 31, 2012 at 9:26 AM, atul anand > wrote: > > > to find kth largest element in the 2 sorted array can be done by simple > > > merge... > > > obv no need for extra space...two indexes will do. > > > > > you just need to check arr1[i...n] == arr2[j..m] > > > > > if(arr1[i] > arr2[j]) > > > { > > >cnt++; > > >index=arr2[j]; > > >j++; > > > > > } > > > else > > > { > > > cnt++; > > > index=arr1[i]; > > > i++; > > > > > } > > > > > if(k==cnt) > > > { > > > print kthe largest element is at position arr[index] > > > break; > > > } > > > > > On Tue, Jan 31, 2012 at 1:15 AM, Ashish Goel > wrote: > > > > >> Hi, > > > > >> I am trying to write code for this problem but having issues. > > >> Can you help > > > > >> Best Regards > > >> Ashish Goel > > >> "Think positive and find fuel in failure" > > >> +919985813081 > > >> +919966006652 > > > > >> -- > > >> You received this message because you are subscribed to the Google > Groups > > >> "Algorithm Geeks" group. > > >> To post to this group, send email to algogeeks@googlegroups.com. > > >> To unsubscribe from this group, send email to > > >> algogeeks+unsubscr...@googlegroups.com. > > >> For more options, visit this group at > > >>http://groups.google.com/group/algogeeks?hl=en. > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Algorithm Geeks" group. > > > To post to this group, send email to algogeeks@googlegroups.com. > > > To unsubscribe from this group, send email to > > > algogeeks+unsubscr...@googlegroups.com. > > > For more options, visit this group at > > >http://groups.google.com/group/algogeeks?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: kth largest element in two sorted arrays
Hey Ashish check this link http://stackoverflow.com/questions/8999610/median-of-lists Thanks and Regards, Venkat Gottipati On Jan 31, 10:14 am, Ashish Goel wrote: > i think this can be done much faster similar to findling median of two > sorted arrays by proceeding with comparing medians of two arrays and then > reducing the data set to approx 3/4th of 2n. I am looking for that algo if > osmeone have. > > Best Regards > Ashish Goel > "Think positive and find fuel in failure" > +919985813081 > +919966006652 > > > > > > > > On Tue, Jan 31, 2012 at 9:26 AM, atul anand wrote: > > to find kth largest element in the 2 sorted array can be done by simple > > merge... > > obv no need for extra space...two indexes will do. > > > you just need to check arr1[i...n] == arr2[j..m] > > > if(arr1[i] > arr2[j]) > > { > > cnt++; > > index=arr2[j]; > > j++; > > > } > > else > > { > > cnt++; > > index=arr1[i]; > > i++; > > > } > > > if(k==cnt) > > { > > print kthe largest element is at position arr[index] > > break; > > } > > > On Tue, Jan 31, 2012 at 1:15 AM, Ashish Goel wrote: > > >> Hi, > > >> I am trying to write code for this problem but having issues. > >> Can you help > > >> Best Regards > >> Ashish Goel > >> "Think positive and find fuel in failure" > >> +919985813081 > >> +919966006652 > > >> -- > >> You received this message because you are subscribed to the Google Groups > >> "Algorithm Geeks" group. > >> To post to this group, send email to algogeeks@googlegroups.com. > >> To unsubscribe from this group, send email to > >> algogeeks+unsubscr...@googlegroups.com. > >> For more options, visit this group at > >>http://groups.google.com/group/algogeeks?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algogeeks@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com. > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: kth largest element in two sorted arrays
Use a binary search. If you have arrays a[n] and b[m], then if you claim that a[i] is the kth largest element, then b[k-i-2] must be larger than a[i], and b[k- i-1] must be less (assuming arrays are zero-based). After using a binary search to find the value of i to meet this condition, you have to determine if a[i] or b[k-i-1] is the final answer. Don On Jan 30, 1:45 pm, Ashish Goel wrote: > Hi, > > I am trying to write code for this problem but having issues. > Can you help > > Best Regards > Ashish Goel > "Think positive and find fuel in failure" > +919985813081 > +919966006652 -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: kth largest element in two sorted arrays
@dave: checkout this link:- http://www.geeksforgeeks.org/archives/2105 algo given in this link has complexity of O(log n) .btw i have doubt if they would work if two array are of different size. for O(log k) ...thinking On Tue, Jan 31, 2012 at 11:06 AM, Dave wrote: > @Atul: Yours is an O(k) algorithm. Is there an O(log k) solution? > > Dave > > On Jan 30, 9:56 pm, atul anand wrote: > > to find kth largest element in the 2 sorted array can be done by simple > > merge... > > obv no need for extra space...two indexes will do. > > > > you just need to check arr1[i...n] == arr2[j..m] > > > > if(arr1[i] > arr2[j]) > > { > >cnt++; > >index=arr2[j]; > >j++; > > > > } > > > > else > > { > > cnt++; > > index=arr1[i]; > > i++; > > > > } > > > > if(k==cnt) > > { > > print kthe largest element is at position arr[index] > > break; > > > > > > > > } > > On Tue, Jan 31, 2012 at 1:15 AM, Ashish Goel wrote: > > > Hi, > > > > > I am trying to write code for this problem but having issues. > > > Can you help > > > > > Best Regards > > > Ashish Goel > > > "Think positive and find fuel in failure" > > > +919985813081 > > > +919966006652 > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Algorithm Geeks" group. > > > To post to this group, send email to algogeeks@googlegroups.com. > > > To unsubscribe from this group, send email to > > > algogeeks+unsubscr...@googlegroups.com. > > > For more options, visit this group at > > >http://groups.google.com/group/algogeeks?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: kth largest element in two sorted arrays
@Atul: Yours is an O(k) algorithm. Is there an O(log k) solution? Dave On Jan 30, 9:56 pm, atul anand wrote: > to find kth largest element in the 2 sorted array can be done by simple > merge... > obv no need for extra space...two indexes will do. > > you just need to check arr1[i...n] == arr2[j..m] > > if(arr1[i] > arr2[j]) > { > cnt++; > index=arr2[j]; > j++; > > } > > else > { > cnt++; > index=arr1[i]; > i++; > > } > > if(k==cnt) > { > print kthe largest element is at position arr[index] > break; > > > > } > On Tue, Jan 31, 2012 at 1:15 AM, Ashish Goel wrote: > > Hi, > > > I am trying to write code for this problem but having issues. > > Can you help > > > Best Regards > > Ashish Goel > > "Think positive and find fuel in failure" > > +919985813081 > > +919966006652 > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algogeeks@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com. > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.