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

  • Home
  • SEARCH
  • 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 7438687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:35:35+00:00 2026-05-29T10:35:35+00:00

I need to find a data structure that meets these requirements: can build it

  • 0

I need to find a data structure that meets these requirements:

  • can build it from a list of n items in O(n)
  • inserting an item takes O(lgn)
  • extracting the median takes O(lgn)
  • extracting the 2nd smallest item takes O(lgn)

For the first three requirements, this works: keep the n/2 smallest items in a max heap and the n/2 largest in a min heap. The roots of those heaps will be the lower/upper median.

But I’m stuck with the 4th requirement. Any ideas?

  • 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. Editorial Team
    Editorial Team
    2026-05-29T10:35:36+00:00Added an answer on May 29, 2026 at 10:35 am

    Keep the n/2 largest items in a min heap. For the n/2 smallest items maintain a pair of max and min heaps. Heaps in this pair are augmented with index of the same element in the paired heap, so that any heap modification updates indexes in the paired heap for all moved items.

    Paired heap explanations

    Both heaps contain exactly the same set of items. Along with each item there is an additional index field. When heap is modified, some items may change their places. If an item is moved from index x to index y, corresponding item in the paired heap must be notified. This corresponding item is easily located in paired heap with moved item’s index field. And the contents of the corresponding item’s index field is changed from x to y. This allows all heap items to know exactly where their pairs are located. Keeping corresponding items in both heaps in-sync allows (while extracting largest item from the max heap or 2nd smallest item from the min heap) extract corresponding item from the paired heap. And keeping heaps in-sync does not increase any of the complexity requirements.

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

Sidebar

Related Questions

I need to find a data structure which I can do with the following
I have N keys. I need to find a data structure which i can
I need to find/create an application that will create employee web usage reports from
I often* find myself in need of a data structure which has the following
Need to find the exact size in bytes, occupied by a tree data structure
I have a table of time-series data of which I need to find all
Consider I have a data grid, I need to find the number of rows
I'm implementing a union-find data structure in C#. The elements must extend the Element
I need to test my data structure (in java) which is like a dictionary
I have a data structure in my C++ program that has some attributes of

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.