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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:25:04+00:00 2026-05-30T19:25:04+00:00

Is there a way to calculate the average distance of array elements from array

  • 0

Is there a way to calculate the average distance of array elements from array average value, by only “visiting” each array element once? (I search for an algorithm)

Example:

Array : [ 1 , 5 , 4 , 9 , 6 ]
Average : ( 1 + 5 + 4 + 9 + 6 ) / 5 = 5
Distance Array : [|1-5|, |5-5|, |4-5|, |9-5|, |6-5|] = [4 , 0 , 1 , 4 , 1 ]
Average Distance : ( 4 + 0 + 1 + 4 + 1 ) / 5 = 2

The simple algorithm needs 2 passes.

1st pass) Reads and accumulates values, then divides the result by array length to calculate average value of array elements.

2nd pass) Reads values, accumulates each one’s distance from the previously calculated average value, and then divides the result by array length to find the average distance of the elements from the average value of the array.

The two passes are identical. It is the classic algorithm of calculating the average of a set of values. The first one takes as input the elements of the array, the second one the distances of each element from the array’s average value.

Calculating the average can be modified to not accumulate the values, but caclulating the average “on the fly” as we sequentialy read the elements from the array.

The formula is:

Compute Running Average of Array's elements
-------------------------------------------
RA[i] = E[i] {for i == 1}
RA[i] = RA[i-1] - RA[i-1]/i + A[i]/i { for i > 1 }

Where A[x] is the array’s element at position x, RA[x] is the average of the array’s elements between position 1 and x (running average).

My question is:

Is there a similar algorithm, to calculate “on the fly” (as we read the array’s elements), the average distance of the elements from the array’s mean value?

The problem is that, as we read the array’s elements, the final average value of the array is not known. Only the running average is known. So calculating differences from the running average will not yield the correct result. I suppose, if such algorithm exists, it probably should have the “ability” to compensate, in a way, on each new element read for the error calculated as far.

  • 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-30T19:25:05+00:00Added an answer on May 30, 2026 at 7:25 pm

    I don’t think you can do better than O(n log n).

    Suppose the array were sorted. Then we could divide it into the elements less than the average and the elements greater than the average. (If some elements are equal to the average, that doesn’t matter.) Suppose the first k elements are less than the average. Then the average distance is

    D = ((xave-x1) + (xave-x2) + (xave-x3) + … + (xave-xk) + (xk+1-xave) + (xk+2-xave) + … + (xn-xave))/n

    = (-x1) + (-x2) + (-x3) + … + (-xk) + (xk+1) + (xk+2) + … + (xn) + (n-2k)xave)/n

    = ( [sum of elements above average] – [sum of elements below average] + (n-2k)xave)/n

    You could calculate this in one pass by working in from both ends, adjusting the limits on the (as-yet-unknown) average as you go. This would be O(n), and the sorting is O(n logn) (and they could perhaps be done in the same operation), so the whole thing is O(n logn).

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

Sidebar

Related Questions

Is there some simple way to calculate a Weeknumber value from a date value
Is there any way to calculate the largest outcome from an Rijndael encryption with
Is there a way to calculate an average of results returned by a subquery
Is there a way to calculate the MD5 hash of a file before the
Is there a convenient way to calculate percentiles for a sequence or single-dimensional numpy
Is there any easy way to calculate the number of lines changed between two
I'm sure there is a quick and easy way to calculate the sum of
I was wonder if there is a simpler (single) way to calculate the remaining
I'm using GMP to calculate very large factorials (e.g. 234234!). Is there any way
Is there way in next piece of code to only get the first record?

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.