Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 133861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:30:32+00:00 2026-05-11T06:30:32+00:00

I need a data structure that always holds the n largest items inserted so

  • 0

I need a data structure that always holds the n largest items inserted so far (in no particular order).

So, if n is 3, we could have the following session where I insert a few numbers and the content of the container changes:

[]  // now insert 1 [1] // now insert 0 [1,0] // now insert 4 [1,0,4] // now insert 3 [1,4,3] // now insert 0 [1,4,3] // now insert 3 [4,3,3] 

You get the idea. What’s the name of the data structure? What’s the best way to implement this? Or is this in some library?

I am thinking to use a container that has a priority_queue for its elements (delegation), which uses the reverse comparison, so pop will remove the smallest element. So the insert function first checks if the new element to be inserted is greater than the smallest. If so, we throw that smallest out and push the new element.

(I have a C++ implementation in mind, but the question is language-agnostic nevertheless.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:30:33+00:00Added an answer on May 11, 2026 at 6:30 am

    The specific datastructure you want is probably the implicit heap. The raw datastructure is just an array; for convenience, say that it is N=2^n elements in size, and that you want to maintain the largest N-1 elements.

    The idea is to treat the array (call it A) as a complete binary tree of depth n:

    • ignore A[0]; treat A[1] as the root node
    • for each node A[k], the children are A[2*k] and A[2*k+1]
    • nodes A[N/2..N-1] are the leaves

    To maintain the tree as a ‘heap’, you need to ensure that each node is smaller than (or equal to) its children. This is called the ‘heap condition’:

    • A[k] <= A[2*k]
    • A[k] <= A[2*k+1]

    To use the heap to maintain the largest N elements:

    • note that the root A[1] is the smallest element in the heap.
    • compare each new element (x) to the root: if it is smaller (x<A[1]), reject it.
    • otherwise, insert the new element into the heap, as follows:
      • remove the root (A[1], the smallest element) from the heap, and reject it
      • replace it with the new element (A[1]:= x)
      • now, restore the heap condition:
        • if x is less than or equal to both of its children, you’re done
        • otherwise, swap x with the smallest child
        • repeat the test&swap at each new position until the heap condition is met

    Basically, this will cause any replacement element to ‘filter up’ the tree until it achieves its natural place. This will take at most n=log2(N) steps, which is as good as you can get. Also, the implicit form of the tree allows a very fast implementation; existing bounded-priority-queue libraries will most likely use an implicit heap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer One of the problems of Windows Mobile is the size… May 11, 2026 at 12:02 pm
  • added an answer SELECT at.author_id, at.author_name, 'article' AS source_table, aat.id FROM author_table at… May 11, 2026 at 12:02 pm
  • added an answer You want a UNIQUE constraint on the table. May 11, 2026 at 12:02 pm

Related Questions

I need to store a tree data structure in my database, for which I
In data processing, I frequently need to create a lookup data structure to map
For an embedded system I need to place a few data structures at fixed
I need a way to easily export and then import data in a MySQL
I need to create a data transfer object, which I will use for storing
I need a way to stash some data that is global to the browser.
I need a programmatic way of creating a SQL Server ODBC Data Source. I
I need a tool or method that allows the transfer of data and automatically
I need to select a bunch of data into a temp table to then
I need to import a huge csv data file (6880 columns) and I need

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.