+ Reply to Thread
Results 1 to 6 of 6

Thread: Find nth largest number in a stream of numbers

  1. #1
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321

    Find nth largest number in a stream of numbers

    Write a program to find nth largest number from a stream of numbers.

  2. #2
    thegeekin Guest
    The best solution to this is to create a max-heap(of n) integers on a running stream of numbers. When a number is read from the stream, we refresh the max-heap to fix the new number from the stream inside the heap. This is a log(n) operation.
    Finding the nth largest number would also be log(n) [if I am not mistaken]

  3. #3
    jalajb2k7 Guest
    finding nth largest in a heap will be n*logn
    correct me if i'm wrong

  4. #4
    jalajb2k7 Guest
    it will be logn only

  5. #5
    bonbon Guest
    I think using min-heap of size n would be better.

    1)Read number from stream and compare it with root in min-heap.
    2)If number read is lesser, throw it.
    3)If number is greater than root, overwrite root with the number and heapify. (O(logn))
    4)After all the numbers are read, the number at the root is nth largest.

    So worst case complexity is O(k*logn) where k is numbers read from stream.

  6. #6
    prodigy Guest

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find 6th largest element in an array
    By Surfer in forum Adobe
    Replies: 4
    Last Post: 5th December 2010, 01:08
  2. Find largest subsequence sum
    By TopGun in forum Algorithm/Data Structure Questions
    Replies: 2
    Last Post: 4th November 2010, 18:59
  3. Find 2nd largest
    By JavaGuy in forum Apple
    Replies: 0
    Last Post: 27th May 2010, 00:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts