返回最大化其整数的子集(平均值 - 中位数)

时间:2018-04-18 06:46:51

标签: algorithm sorting subset mean median

给出一组整数作为输入。您必须返回该集合的子集,以便该子集的均值 - 中位数值最大。

示例1

输入

{1,2,3,4} 

输出

{1,2,4}

示例2

输入

{1,2,2,3,3}

输出

{2,2,3}

9 个答案:

答案 0 :(得分:1)

对于每个可能的中位数:

lllllmrrrrr

对L和R两部分进行排序,然后开始从两个部分中选择对lr个最大元素,并添加每个下一个元素重新计算均值,存储具有最佳差异的排列。然后对于最小元素也一样。

大约有N个可能的中位数,排序需要O(N*lgN),在您需要计算的每次迭代中N意味着,您可以在O(N)中执行此操作。因此,总体复杂度为O(N^3*LgN),但很可能您可以避免在每次迭代时进行排序,而是仅对整个数组进行一次排序,并在每次迭代时更新O(1)中的部分。通过这样的改进,它是O(N^2)

答案 1 :(得分:1)

这个问题最重要的是找到子集。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
    
public class MeanMedian {
    public static void main(String[] args) {

        int[] arr = { 1, 2, 3 };// { 1, 2, 2, 3, 3 };// { 1, 2, 3, 4 };

        returnMaxMeanMedian(arr);

    }

    private static void returnMaxMeanMedian(int[] arr) {
        double max = -999.9;

        List<Integer[]> subArr = subSet(arr);

        Integer[] maxArr = new Integer[1];

        for (Integer[] sub : subArr) {

            double newMax = calcDiff(sub);

            if (max <= newMax) {
                max = newMax;
                maxArr = sub;
            }
        }
        System.out.println(Arrays.toString(maxArr));
    }

    private static double calcDiff(Integer[] sub) {
        // calc. mean
        double sum = 0;
        for (int i = 0; i < sub.length; i++) {
            sum += sub[i];
        }
        sum = sum / sub.length;

        // calc. median
        double median = 0;
        if (sub.length % 2 == 0)
            median = (double) (sub[(sub.length / 2) - 1] + sub[sub.length / 2]) / 2;
        else
            median = sub[sub.length / 2];

        double diff = sum - median;
        return diff;
    }

    private static List<Integer[]> subSet(int[] arr) {
        List<Integer[]> subArr = new ArrayList<Integer[]>();

        int n = arr.length;

        // Run a loop until 2^n
        // subsets one by one
        for (int i = 0; i < (1 << n); i++) {

            String subSet = "";
            // Print current subset
            for (int j = 0; j < n; j++)

                if ((i & (1 << j)) > 0)
                    subSet += arr[j] + " ";

            subArr.add(convertToInt(subSet.trim().split(" ")));
        }
        return subArr;
    }

    private static Integer[] convertToInt(String[] arr) {

        if (arr[0] == "")
            return new Integer[] { 0 };

        Integer[] intArr = new Integer[arr.length];

        for (int i = 0; i < arr.length; i++) {
            intArr[i] = Integer.parseInt(arr[i].trim());
        }

        return intArr;
    }
}

答案 2 :(得分:0)

在O(n log n)中对列表进行排序。

删除中位数左侧的任何元素(中心元素或对)对中位数具有相同的影响,但不同地影响均值。对于右边的元素也同样如此。

这意味着如果有任何改进(平均值 - 中位数),其中一个会改善它:

  1. 数组中的最小元素
  2. 中位数右侧的最小元素
  3. 包含中位数
  4. 的元素之一

    即,对于每个可能的新中位数,我们如何才能达到最大均值?

    反复检查这些3-4以改善平均中位数,删除任何改善最多的东西。每个操作都是O(1),重新计算平均值和中位数。你必须在最多O(n)次这样做。

    如果列表未排序,则运行时间为O(n log n),否则为O(n)。

答案 3 :(得分:0)

这个问题仅适用于正序数字吗?如果是的话,我写了这段有效的代码:

mytree.selection()

此代码的顺序为O(n)(如果我们忽略了对输入数组进行排序的必要性)。

如果还需要-ve输入 - 唯一的出路是评估每个子集。这种方法的缺点是算法具有指数级:O(2 ^ n)。

作为折衷方案,您可以在代码中使用两种类型的算法,并通过评估输入序列在两者之间切换。 顺便问一下,你在哪里遇到过这个问题?

答案 4 :(得分:0)

from itertools import combinations

[Verfication of the code][1]
# function to generate all subsets possible, there will be 2^n - 1 subsets(combinations)
def subsets(arr):
    temp = []
    for i in range(1, len(arr)+1):
        comb = combinations(arr, i)
        for j in comb:
            temp.append(j)
    return temp

# function to calculate median
def median(arr):
    mid = len(arr)//2
    if(len(arr)%2==0):
        median = (arr[mid] + arr[mid-1])/2
    else:`
        median = arr[mid]
    return median

# function to calculate median
def mean(arr):
    temp = 0
    for i in arr:
        temp = temp + i
    return temp/len(arr)

# function to solve given problem
def meanMedian(arr):
    sets = subsets(arr)
    max_value = 0
    for i in sets:
        mean_median = mean(i)-median(i)
        if(mean_median>max_value):
            max_value = mean_median
            needed_set = i
    return needed_set



  [1]: https://i.stack.imgur.com/Mx4pc.png

答案 5 :(得分:0)

所以我对这个问题做了一些尝试,下面的代码可能会对您有所帮助。它的编写方式应该易于阅读,如果没有,请告诉我。也许您需要从用户那里获取数组输入,因为我已经选择了固定数组。我确定这应该不是什么大问题。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class MeanMinusMedian 
{
    private static float mean = 0;
    private static float median = 0;
    private static float meanMinusMedian = 0;
    private static List<Integer> meanMinusMedianList = null;

    private static void formMeanMinusMedianArr(int data[], int sumOfData) 
    {
        findMean(data, sumOfData);
        findMedian(data);
        if ((mean - median) > meanMinusMedian) {
            meanMinusMedian = mean - median;
            meanMinusMedianList = new ArrayList<Integer>();
            Arrays.stream(data) 
            .forEach(e->meanMinusMedianList.add(e));
        }
    }

/**
 * @param data
 */
private static void findMedian(int[] data) {
    int dataLen = data.length;
    median = data.length % 2 == 0 ? ((float)data[dataLen / 2] + (float)data[dataLen / 2 - 1]) / 2 : data[dataLen / 2];
}

/**
 * @param data
 * @param sumOfData
 */
private static void findMean(int[] data, int sumOfData) {
    mean = ((float)sumOfData /(float) data.length);
}

/**
 * 
 * @param arr
 * @param data
 * @param start
 * @param end
 * @param index
 * @param runningVal
 */
private static void combinationUtil(int arr[], int data[], int start, int end, int index, int runningVal) {
    // Current combination is ready to be printed, print it
    if (index == runningVal) {
        formMeanMinusMedianArr(data, Arrays.stream(data) // Step 1 
                  .sum());
        return;
    }
    // replace index with all possible elements. The condition
    // "end-i+1 >= r-index" makes sure that including one element
    // at index will make a combination with remaining elements
    // at remaining positions
    for (int i = start; i <= end && end - i + 1 >= runningVal - index; i++) {
        data[index] = arr[i];
        combinationUtil(arr, data, i + 1, end, index + 1, runningVal);
    }
}

/**
 * 
 * @param arr
 * @param n
 * @param runningVal
 */
private static void printCombination(int arr[], int n, int runningVal) {
    int data[] = new int[runningVal];
    // Print all combination using temporary array 'data[]'
    combinationUtil(arr, data, 0, n - 1, 0, runningVal);
}

public static void main(String[] args) {
    int arr[] = { 1, 2, 2, 3, 3 };
    int runningVal = 1;//Running value
    int len = arr.length;
    for (int i = 1; i < arr.length; i++) {
        printCombination(arr, len, runningVal + i);
    }
    System.out.println(meanMinusMedianList);
}

}

答案 6 :(得分:0)

package subsetMean_Median;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MySolution {
    public static void main(String[] args) {
        int[] arr= 
            {2,3,2,1,3};
//          {1,3,2,4};
        Arrays.sort(arr);
        int[] outp=meanMedian(arr);
        for(int e:outp) {
            System.out.print(e+"\t");
        }
    }

    protected static int[] meanMedian(int[] arr) {
        double median=findMedian(arr);
        double mean=findMean(arr);
        double diff=median-mean;
        int MAXINDEX=0;
        int n=arr.length;
        double sets=(1<<n);
        System.out.println("sets:"+sets);
        for(int i=1;i<=sets;i++) {
            int[] subset=findSubset(i,arr);
            mean=findMean(subset);
            median=findMedian(subset);
            if(mean -median>diff) {
                diff=mean-median;MAXINDEX=i;
            }
        }
        System.out.println("mean: "+mean+"\tmedian: "+median+"\tdiff: "+diff);
        return findSubset(MAXINDEX,arr);
    }
    protected static int[] findSubset(int counter, int[] arr) {
        int n=arr.length;
        List<Integer> ls=new ArrayList<Integer>();
        for(int j=0;j<n;j++) {
            if((counter & (1<<j))>0) {
                ls.add(arr[j]);
            }
        }
        int[] output= new int[ls.size()];
        for(int j=0;j<ls.size();j++) {
            output[j]=ls.get(j);
        }
        return output;
    }

    protected static double findMean(int[] arr) {
        int n=arr.length;
        double sum=0;
        if(n==0) return 0;
        for(int i=0;i<n;i++)
            sum +=arr[i];
        return (sum/n);
    }

    protected static double findMedian(int[] arr) {
        int n=arr.length;
        if(n%2==1)
            return arr[(n/2)];
        else if(n>=2)
            return 0.5*(arr[((n-2)/2)]+arr[n/2]);
        else return 0;
    }
}


答案 7 :(得分:0)

参考Bhaskar13 https://stackoverflow.com/a/59386801/3509609的答案,我解决了它,而不使用位移运算符,以增加可读性。

package array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class MeanMinusMedianMax {

    public static void main(String[] args) {
        System.out.println(Arrays.toString(maxDiffrenceSubSet(4, new int[] { 4, 2, 3, 1 })));
        System.out.println(Arrays.toString(maxDiffrenceSubSet(4, new int[] { 1, 2, 2, 3, 3 })));
    }

    public static int[] maxDiffrenceSubSet(int n, int[] input2) {

        int totalSubsets = (int) Math.pow(2, n);
        Map<Integer, ArrayList<Integer>> subsetsMap = new HashMap<Integer, ArrayList<Integer>>();
        Integer maxKey = null;
        double maxDiff = 0;

        for (int i = 0; i < totalSubsets; i++) {
            String binaryString = Integer.toBinaryString(i);
            while (binaryString.length() < 4) {
                binaryString = "0" + binaryString;
            }
            char[] currentPick = binaryString.toCharArray();
            ArrayList<Integer> currentList = new ArrayList<Integer>();
            for (int x = 0; x < currentPick.length; x++) {
                if ((currentPick[x]) == '1') {
                    currentList.add(input2[x]);
                }
            }
            Collections.sort(currentList);
            subsetsMap.put(i, currentList);
            double mean = findMean(currentList);
            double median = findMedian(currentList);
            double currentDifference = mean - median;
            if (currentDifference > maxDiff) {
                maxDiff = currentDifference;
                maxKey = i;
            }
        }

        return subsetsMap.get(maxKey).stream().mapToInt(i -> i).toArray();
    }

    static double findMean(ArrayList<Integer> arr) {

        int n = arr.size();
        double sum = 0;
        if (n == 0)
            return 0;
        for (int i = 0; i < n; i++)
            sum += arr.get(i);
        return (sum / n);
    }

    static double findMedian(ArrayList<Integer> arr) {
        int n = arr.size();
        if (n % 2 == 1)
            return arr.get((n / 2));
        else if (n >= 2)
            return 0.5 * (arr.get(((n - 2) / 2)) + arr.get(n / 2));
        else
            return 0;
    }
}

答案 8 :(得分:0)

class usermaincode (object):
    def meanmeridian(cls,ip1,ip2=[]):
        s = []
        s = ip2
        lst = []
        final = []
        op = []
        max_val = 0
        diff  = 0
        for i in range(1,ip1+1):
            n=i
            lst = list(itertools.combinations(s,n))
            final = final +lst      
        for i in range(len(final)):
            men = statistics.mean(final[i])
            med = statistics.median(final[i])
            diff = men - med
            if max_val < diff:
                op = final[i]
                max_val = diff
        return op