📌  相关文章
📜  将数组分成两个奇数长度组,它们的中位数之间的绝对差异最小

📅  最后修改于: 2021-09-03 03:58:29             🧑  作者: Mango

给定一个由偶数长度的正整数组成的数组arr[] ,任务是将arr[] 的这些元素分成两组,每组的长度为奇数,以使两组中值之间的绝对差异最小化。
例子:

方法:

  • 如果给定的阵列ARR []进行排序,ARR的中间元件[]将给出最小差值。
  • 因此,将arr[]以这样的方式划分,即这两个元素将是两个奇数长度的新数组的中值。
  • 因此,把ARR []的第n / 2元件中的第一组中和第(n / 2 – 1)第二组中的ARR []分别作为中值的第i个元素。
  • 那么abs(arr[n/2] – arr[(n/2)-1])是两个新数组之间的最小差异。

下面是上述方法的实现:

C++
// C++ program to minimise the
// median between partition array
 
#include "bits/stdc++.h"
using namespace std;
 
// Function to find minimise the
// median between partition array
int minimiseMedian(int arr[], int n)
{
    // Sort the given array arr[]
    sort(arr, arr + n);
 
    // Return the difference of two
    // middle element of the arr[]
    return abs(arr[n / 2] - arr[(n / 2) - 1]);
}
 
// Driver Code
int main()
{
    int arr[] = { 15, 25, 35, 50 };
 
    // Size of arr[]
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function that returns the minimum
    // the absolute difference between
    // median of partition array
    cout << minimiseMedian(arr, n);
    return 0;
}


Java
// Java program to minimise the
// median between partition array
import java.util.*;
 
class GFG
{
 
    // Function to find minimise the
    // median between partition array
    static int minimiseMedian(int arr[], int n)
    {
        // Sort the given array arr[]
        Arrays.sort(arr);
     
        // Return the difference of two
        // middle element of the arr[]
        return Math.abs(arr[n / 2] - arr[(n / 2) - 1]);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr[] = { 15, 25, 35, 50 };
     
        // Size of arr[]
        int n = arr.length;
     
        // Function that returns the minimum
        // the absolute difference between
        // median of partition array
        System.out.println(minimiseMedian(arr, n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to minimise the
# median between partition array
 
# Function to find minimise the
# median between partition array
def minimiseMedian(arr, n) :
 
    # Sort the given array arr[]
    arr.sort();
     
    # Return the difference of two
    # middle element of the arr[]
    ans = abs(arr[n // 2] - arr[(n // 2) - 1]);
     
    return ans;
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 15, 25, 35, 50 ];
 
    # Size of arr[]
    n = len(arr);
 
    # Function that returns the minimum
    # the absolute difference between
    # median of partition array
    print(minimiseMedian(arr, n));
     
# This code is contributed by AnkitRai01


C#
// C# program to minimise the
// median between partition array
using System;
 
class GFG
{
 
    // Function to find minimise the
    // median between partition array
    static int minimiseMedian(int []arr, int n)
    {
        // Sort the given array []arr
        Array.Sort(arr);
     
        // Return the difference of two
        // middle element of the []arr
        return Math.Abs(arr[n / 2] - arr[(n / 2) - 1]);
    }
     
    // Driver Code
    public static void Main(String[] args)
    {
        int []arr = { 15, 25, 35, 50 };
     
        // Size of []arr
        int n = arr.Length;
     
        // Function that returns the minimum
        // the absolute difference between
        // median of partition array
        Console.WriteLine(minimiseMedian(arr, n));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
10

时间复杂度: O(N*log N)