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 9107669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:37:41+00:00 2026-06-17T02:37:41+00:00

I am pondering the solution to the following problem: I have a large array

  • 0

I am pondering the solution to the following problem: I have a large array of integers (in even tougher case a stream of integers) and I want to convert that array into and array of medians, tht is it position corresponds to median of array [0..i].

Now, my brute force approach would be for each subarray, sort it and then select middle element. That is O(n^2 log n), as each n subarrays must be sorted in N log N time. I could probably lower the time to N^2 using some counting mechanism, but is N^2 the best that can be done?

  • 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-06-17T02:37:42+00:00Added an answer on June 17, 2026 at 2:37 am

    Inserting a single item into an already sorted tree-like structure, like e.g. a red-black tree, will take O(log n). If you maintain the number of descendants for every node, then finding the median can be done in O(log n) as well. Do so for all n elements of your stream, and you have an O(n log n) algorithm.

    The data structure and median computation could look something like this:

    struct TreeNode {
      enum { RED, BLACK } color;
      size_t numElements;
      int value;
      TreeNode* left, right;
    } *root;
    
    TreeNode* treeSelect(TreeNode *top, size_t count) {
      if (top->left != NULL) {
        if (count < top->left->numElements)
          return treeSelect(top->left, count)
        count -= top->left->numElements;
      }
      if (count == 0)
        return top;
      return treeSelect(top->right, count - 1);
    }
    
    TreeNode* treeMedian() {
      return treeSelect(root, root->numElements / 2);
    }
    

    The other operations are those usually used for red-black trees, although you can skip those for removal of nodes. You can adjust them to work with duplicate elements. The general rule here is that when an element to be inserted is equal to the element of the current node, you may insert into any child tree. And when balancing the tree, the order of duplicate keys should be maintained, so that you maintain the order of the attached subtrees as well. But unless I miss something, balancing will work without value comparisons in any case, so once you’ve inserted duplicates, you are done. If you expect really many duplicate values, you might use a multimap-like approach instead, with a counter in each node.

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

Sidebar

Related Questions

I am working on the following problem: I have to convert a given array
I have a problem which has an solution that can be solved by iteration,
I've been pondering this problem for a while and can't find the solution (It
I'm trying to solve the following problem: Say I have a Python script (let's
I was wondering if anyone had a possible solution to the following problem... I
Problem I need a key-value store that can store values of the following form:
I was wondering if there is any nice solution for the following problem: Assuming
I have the following problem in AS3. I have a string like this one:
I have a set of numbers produced using the following formula with integers 0
I have the following problem: I'm storing a list of items in the database

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.