-1

I have an array of values (probabilities) as below,

  a = [0, 0.1, 0.9, 0.2, 0, 0.8, 0.7, 0]

To select the maximum value from this array I can use the generalized mean equation. But what should I do if I want to select say top N values and sum them?

e.g. summing top 2 values will give me 0.9 + 0.8 = 1.7

**** But I dont need an implementation/algorithm for it. I need a mathematical equation (e.g. generalized mean for selecting the max value), so that I want to optimize a function which includes this selection.

user570593
  • 3,420
  • 12
  • 56
  • 91
  • How large can `a` get? If it's not too big, a simple sort and slice should work. – Nelewout Jan 08 '16 at 18:16
  • 1
    Possible duplicate of [Finding the first n largest elements in an array](http://stackoverflow.com/questions/7272534/finding-the-first-n-largest-elements-in-an-array) – andand Jan 08 '16 at 18:24
  • Is the individual values important, or just the sum? – SGM1 Jan 08 '16 at 19:20

1 Answers1

0

Effective way to get top K values is using of some K-select algorithm.
For example, fast and reliable one is Quickselect (based on the same partition procedure as QuickSort), which has average complexity O(N).

generalized mean with regard to maximum seems pure mathematical conception, doesn't it?
So I doubt that sum of infinite powers is applicable in real life (programming).

MBo
  • 77,366
  • 5
  • 53
  • 86