GeeksforGeeks » Algorithms

Print k largest numbers

(3 posts)
[closed]
  • Started 2 years ago by geek4u
  • Latest reply from geeksforgeeks
  1. geek4u
    Member
    Posted 2 years ago #

    Write an efficient program for printing k largest elements in an array. Elements in array can be in any order.

  2. Shilpi
    Member
    Posted 2 years ago #

    Following approaches can be used:
    1) Sort the elements in descending order. Print the first k numbers of the sorted array.
    Time complexity: O(nlogn)

    2) Build a max-heap tree and call extract-max k times
    Time complexity: O(n + klogn)

  3. GeeksforGeeks
    Key Master
    Posted 2 years ago #

    @geek4u and @Shilpi: This has been published. Please see http://geeksforgeeks.org/?p=2392.

    We have added one more method that uses order statistics.

    Thanks for your contribution to the portal. Keep it up!!


Topic Closed

This topic has been closed to new replies.

RSS feed for this topic