If I'm right you want a subset which has k elements and the difference of the smallest and the largest element is minimum.
You can do it in O( Nlog(N) ). *Sort input* in ascending order, then iterate through the array and keep the difference of the last k elements. O( nlogn + n ) belongs to O(nlogn) 2 3 5 6 9 11 two iterators, first on 2, second on 5, then each time ++ both iterators and check the difference of these two elements, pick the minimum among all these values. On Fri, Nov 18, 2011 at 5:00 PM, Zyro <vivkum...@gmail.com> wrote: > Q: Select the K elements in an array of size N which are having the > minimum difference among them? > For Example : If you have an array like arr[]={9,5,2,6,3,11} and value > of K is 3. Then ans would be {2,3,5}. > > -- > 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. > > -- MeHdi KaZemI -- 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.