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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:35:23+00:00 2026-06-06T00:35:23+00:00

I have an array lets say a = { 1,4,5,6,2,23,4,2}; now I have to

  • 0

I have an array lets say a = { 1,4,5,6,2,23,4,2};
now I have to find median of array position from 2 to 6 (odd total terms), so what I have done, I have taken a[1] to a[5] in arr[0] to arr[4] then I have sorted it and write the arr[2] as the median .

But here every time I put values from one array to another, so that the values of my initial array remains the same. Secondly, I have sorted, so this procedure is taking pretty much **time**.
So I want to know if there is any way I can do this differently to reduce my computation time.

Any websites, material to understand, what, and how to do?

  • 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-06T00:35:24+00:00Added an answer on June 6, 2026 at 12:35 am

    If you are doing multiple queries on the same array then you could use a Segment Tree. They are generally used to do range minimum/maximum and range sum queries but you can change it to do range median.

    A segment tree for a set with n intervals uses O(n log n) storage and can be built in O(n log n) time. A range query can be done in O(log n).

    Example of median in range segment tree:

    You build the segment tree from the bottom up (update from the top down):

                        [5]
            [3]                     [7]
     [1,2]        [4]         [6]         [8] 
    1     2     3     4     5     6     7     8
    

    Indices covered by node:

                        [4]
            [2]                     [6]
     [0,1]        [3]         [5]         [7] 
    0     1     2     3     4     5     6     7
    

    A query for median for range indices of 4-6 would go down this path of values:

                        [4]
                              [5]
    0     1     2     3     4     5     6     7
    

    Doing a search for the median, you know the number of total elements in the query (3) and the median in that range would be the 2nd element (index 5). So you are essentially doing a search for the first node which contains that index which is node with values [1,2] (indices 0,1).

    Doing a search of the median of the range 3-6 is a bit more complicated because you have to search for two indices (4,5) which happen to lie in the same node.

                        [4]
                                    [6]
                              [5] 
    0     1     2     3     4     5     6     7
    

    Segment tree

    Range minimum query on Segment Tree

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

Sidebar

Related Questions

Lets say I have an array like int arr[10][10]; Now i want to initialize
Lets say I have an array with a structure like this: $arr= Array( array(
Lets say I have an array which has n dimensions. Now in order to
Lets say I have an array $aSomeArray = array(1,2,3,4,5,6,7,8,9,10,11,12); Now displaying one array value
Ok so lets say I have an array that can be 0 - X
lets say i have one array a = numpy.arange(8*6*3).reshape((8, 6, 3)) #and another: l
Lets say I have an array numbers that contains the following values: int numbers
Lets say I have an array of length n and I want to pick
Lets say I have an array : a=[[1,2,3],[4,5]] and I have another array :
Lets say I have the following array in JavaScript: var skins = new Array('Light',

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.