Java Array Sum sumMaxK(double[] x, int k)

Here you can find the source of sumMaxK(double[] x, int k)

Description

sum Max K

License

Apache License

Declaration

public static double sumMaxK(double[] x, int k) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static double sumMaxK(double[] x, int k) {
        int[] ids = sort(x);
        if (k > ids.length) {
            k = ids.length;/*  w w w.  j av a2s  . c om*/
        }
        double s = 0;
        for (int i = ids.length - 1; (ids.length - 1 - i + 1) <= k; i--) {
            s = s + x[ids[i]];
        }
        return s;
    }

    public static int[] sort(double[] x) {
        int[] id = new int[x.length];
        boolean[] fid = new boolean[id.length];
        int cnt = 0;
        for (int i = 0; i < id.length; i++) {
            int mi = 0;
            double min = x[mi];
            for (int j = 0; j < x.length; j++) {
                if (fid[j]) {
                    continue;
                }
                if (fid[mi]) {
                    mi = j;
                    min = x[j];
                } else {
                    if (min > x[j]) {
                        mi = j;
                        min = x[j];
                    }
                }
            }
            fid[mi] = true;
            id[cnt++] = mi;
        }
        return id;
    }
}

Related

  1. sumLog10(double[] log10values)
  2. sumLong(int[] array)
  3. summarizeIntegerArray(int[] numbers)
  4. summarizeNumbersAsString(int[] numbers)
  5. summation(double[] values)
  6. summedDifference(int[][] matrix, short topX, short botX, short row)
  7. sumMinMax(double[] values, int out[])
  8. sumMult(double[] aArray1, double[] aArray2)
  9. sumNaive(final double... values)