i am Considering given heap as min heap Sol - because heap has property that root will has lower value than all the elements in its left sub tree and right sub tree so in main we will call a function passing root and value k and x if at any time root is greater than x and k > 0 that means rest of the elements are greater than x so kth is also greater than x
else make recursive calls for both of its child as soon as k hits zero in any recursive call we know that there are k elements less than x. i think in worst case 2k comparisons will be there hence O(k) On Wed, Dec 14, 2011 at 12:24 PM, atul anand <atul.87fri...@gmail.com>wrote: > yup rite...it should be O(k log n ) not O(n log n). > > > On Wed, Dec 14, 2011 at 11:44 AM, Dave <dave_and_da...@juno.com> wrote: > >> @Atul: The initial heap is given. You have to maintain the heap >> property as k elements are removed, which is O(k log n). This does not >> satisfy the original request for an algorithm that is "O(k) in the >> worst-case, independent of the size of the heap." >> >> Dave >> >> On Dec 13, 10:31 pm, atul anand <atul.87fri...@gmail.com> wrote: >> > @gaurav : you need to first build heap and then maintain heap property >> ever >> > time you remove element.so this would take O(n logn ). >> > >> > >> > >> > On Wed, Dec 14, 2011 at 1:38 AM, Gaurav Kumar <gkuma...@gmail.com> >> wrote: >> > > Why can't we keep removing the minimum element each time and compare >> it >> > > with x? This should take O(k) time since in a Min heap, the minimum >> element >> > > can be removed in O(1) time? Am I missing something? >> > >> > > On Tue, Dec 13, 2011 at 8:43 AM, atul anand <atul.87fri...@gmail.com >> >wrote: >> > >> > >> O(k) in the worst-case , then i guess it would better to use >> > >> median-of median algo to find element at rank k. and comparing with >> x. >> > >> > >> or >> > >> we can us hashtable to solve this. >> > >> > >> On Tue, Dec 13, 2011 at 3:23 PM, Ankur Garg <ankurga...@gmail.com> >> wrote: >> > >> > >>> Given an array-based heap on n elements and a real number x, >> efficiently >> > >>> determine whether the kth smallest element in the heap is greater >> than or >> > >>> equal to x. Your algorithm should be O(k) in the worst-case, >> independent of >> > >>> the size of the heap. >> > >> > >>> This question was also asked in Amazon >> > >> > >>> -- >> > >>> 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. >> >> > -- > 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. > -- Sunny Aggrawal B.Tech. V year,CSI Indian Institute Of Technology,Roorkee -- 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.