📌  相关文章
📜  最小化 x 的值,使 |a1−x|^c+|a2−x|^c+···+|an−x|^c 的值最小化 c 的值为 1 和 2

📅  最后修改于: 2022-05-13 01:56:05.327000             🧑  作者: Mango

最小化 x 的值,使 |a1−x|^c+|a2−x|^c+···+|an−x|^c 的值最小化 c 的值为 1 和 2

给定一个包含N个元素的数组arr[] ,任务是找到x的值,它使 c = 1 的表达式的值最小。

例子:

方法:在一般情况下, x的最佳选择是 给定数字的中位数,中位数是最佳选择,因为如果x小于中位数,则通过增加x和会变小,如果x大于中位数,则通过减少x会变小。因此,最优解是x是中位数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the possible
// values of x that minimizes the sum
void findX(int arr[], int n)
{
    // Sort the numbers
    sort(arr, arr + n);
 
    // Stores the median
    int x;
 
    // Only one median if n is odd
    if (n % 2 != 0) {
        x = arr[n / 2];
    }
 
    // Two medians if n is even
    // and every value between them
    // is optimal print any of them
    else {
        int a = arr[n / 2 - 1];
        int b = arr[n / 2];
        x = a;
    }
 
    int sum = 0;
 
    // Find minimum sum
    for (int i = 0; i < n; i++) {
        sum += abs(arr[i] - x);
    }
 
    cout << sum;
}
 
// Driver Code
int main()
{
    int arr1[] = { 1, 2, 9, 2, 6 };
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
 
    findX(arr1, n1);
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
 
class GFG
{
   
  // Function to print the possible
// values of x that minimizes the sum
static void findX(int arr[], int n)
{
   
    // Sort the numbers
    Arrays.sort(arr);
 
    // Stores the median
    int x;
 
    // Only one median if n is odd
    if (n % 2 != 0) {
        x = arr[(int)Math.floor(n / 2)];
    }
 
    // Two medians if n is even
    // and every value between them
    // is optimal print any of them
    else {
        int a = arr[n / 2 - 1];
        int b = arr[n / 2];
        x = a;
    }
 
    int sum = 0;
 
    // Find minimum sum
    for (int i = 0; i < n; i++) {
        sum += Math.abs(arr[i] - x);
    }
 
   System.out.println( sum);
}
 
    public static void main (String[] args) {
         
    int arr1[] = { 1, 2, 9, 2, 6 };
    int n1 = arr1.length;
 
    findX(arr1, n1);
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python program for the above approach
 
# Function to print the possible
# values of x that minimizes the sum
def findX(arr, n):
   
  # Sort the numbers
  arr.sort();
 
  # Stores the median
  x = None;
 
  # Only one median if n is odd
  if (n % 2 != 0):
    x = arr[n // 2];
   
  # Two medians if n is even
  # and every value between them
  # is optimal print any of them
  else:
    a = arr[(n // 2) - 1];
    b = arr[n // 2];
    x = a;
  sum = 0;
 
  # Find minimum sum
  for i in range(n):
    sum += abs(arr[i] - x);
 
 
  print(sum);
 
# Driver Code
arr1 = [1, 2, 9, 2, 6];
n1 = len(arr1)
 
findX(arr1, n1);
 
# This code is contributed by gfgking.


C#
// C# code for the above approach
using System;
 
class GFG {
 
    // Function to print the possible
    // values of x that minimizes the sum
    static void findX(int[] arr, int n)
    {
 
        // Sort the numbers
        Array.Sort(arr);
 
        // Stores the median
        int x;
 
        // Only one median if n is odd
        if (n % 2 != 0) {
            x = arr[(int)Math.Floor((float)(n / 2))];
        }
 
        // Two medians if n is even
        // and every value between them
        // is optimal print any of them
        else {
            int a = arr[n / 2 - 1];
 
            x = a;
        }
 
        int sum = 0;
 
        // Find minimum sum
        for (int i = 0; i < n; i++) {
            sum += Math.Abs(arr[i] - x);
        }
 
        Console.WriteLine(sum);
    }
 
    public static void Main(string[] args)
    {
 
        int[] arr1 = { 1, 2, 9, 2, 6 };
        int n1 = arr1.Length;
 
        findX(arr1, n1);
    }
}
 
// This code is contributed by ukasp.


Javascript


C++
// C++ implementation for the above approach
#include 
using namespace std;
 
// Function to find the value of x
// that minimizes the sum
void findX(int arr[], int n)
{
    // Store the sum
    double sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
 
    // Store the average of numbers
    double x = sum / n;
 
    double minSum = 0;
 
    // Find minimum sum
    for (int i = 0; i < n; i++) {
        minSum += pow((arr[i] - x), 2);
    }
 
    cout << minSum;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 9, 2, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    findX(arr, n);
 
    return 0;
}


Java
// Java implementation for the above approach
import java.util.*;
public class GFG
{
// Function to find the value of x
// that minimizes the sum
static void findX(int []arr, int n)
{
    // Store the sum
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
 
    // Store the average of numbers
    int x = sum / n;
 
    int minSum = 0;
 
    // Find minimum sum
    for (int i = 0; i < n; i++) {
        minSum += Math.pow((arr[i] - x), 2);
    }
 
    System.out.print(minSum);
}
 
// Driver Code
public static void main(String args[])
{
    int []arr = { 1, 2, 9, 2, 6 };
    int n = arr.length;
 
    findX(arr, n);
}
}
// This code is contributed by Samim Hossain Mondal.


Python3
# Python implementation for the above approach
 
# Function to find the value of x
# that minimizes the sum
def findX(arr, n):
   
    # Store the sum
    sum = 0;
    for i in range(n):
        sum += arr[i];
     
    # Store the average of numbers
    x = sum // n;
 
    minSum = 0;
 
    # Find minimum sum
    for i in range(n):
        minSum += pow((arr[i] - x), 2);
    print(minSum);
 
# Driver Code
if __name__ == '__main__':
    arr = [ 1, 2, 9, 2, 6 ];
    n = len(arr);
 
    findX(arr, n);
 
# This code is contributed by shikhasingrajput


C#
// C# implementation for the above approach
using System;
class GFG
{
// Function to find the value of x
// that minimizes the sum
static void findX(int []arr, int n)
{
    // Store the sum
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
 
    // Store the average of numbers
    int x = sum / n;
 
    int minSum = 0;
 
    // Find minimum sum
    for (int i = 0; i < n; i++) {
        minSum += (int)Math.Pow((arr[i] - x), 2);
    }
 
    Console.Write(minSum);
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 2, 9, 2, 6 };
    int n = arr.Length;
 
    findX(arr, n);
}
}
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
12

时间复杂度:O(N*log N)
辅助空间:O(1)

给定一个包含N个元素的数组arr[] ,任务是找到x的值,它使c = 2的表达式的值最小。

例子 :

方法:在一般情况下, x的最佳选择是数字的平均值。这个结果可以通过将总和展开如下:

最后一部分不依赖于x 。其余部分形成一个函数nx 2 - 2xs ,其中s=a 1 +a 2 +···+a n对这个方程应用导数wrt x并将结果等同于零给我们x = s / n ,这是使总和最小化的值。

下面是上述方法的实现:

C++

// C++ implementation for the above approach
#include 
using namespace std;
 
// Function to find the value of x
// that minimizes the sum
void findX(int arr[], int n)
{
    // Store the sum
    double sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
 
    // Store the average of numbers
    double x = sum / n;
 
    double minSum = 0;
 
    // Find minimum sum
    for (int i = 0; i < n; i++) {
        minSum += pow((arr[i] - x), 2);
    }
 
    cout << minSum;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 9, 2, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    findX(arr, n);
 
    return 0;
}

Java

// Java implementation for the above approach
import java.util.*;
public class GFG
{
// Function to find the value of x
// that minimizes the sum
static void findX(int []arr, int n)
{
    // Store the sum
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
 
    // Store the average of numbers
    int x = sum / n;
 
    int minSum = 0;
 
    // Find minimum sum
    for (int i = 0; i < n; i++) {
        minSum += Math.pow((arr[i] - x), 2);
    }
 
    System.out.print(minSum);
}
 
// Driver Code
public static void main(String args[])
{
    int []arr = { 1, 2, 9, 2, 6 };
    int n = arr.length;
 
    findX(arr, n);
}
}
// This code is contributed by Samim Hossain Mondal.

Python3

# Python implementation for the above approach
 
# Function to find the value of x
# that minimizes the sum
def findX(arr, n):
   
    # Store the sum
    sum = 0;
    for i in range(n):
        sum += arr[i];
     
    # Store the average of numbers
    x = sum // n;
 
    minSum = 0;
 
    # Find minimum sum
    for i in range(n):
        minSum += pow((arr[i] - x), 2);
    print(minSum);
 
# Driver Code
if __name__ == '__main__':
    arr = [ 1, 2, 9, 2, 6 ];
    n = len(arr);
 
    findX(arr, n);
 
# This code is contributed by shikhasingrajput

C#

// C# implementation for the above approach
using System;
class GFG
{
// Function to find the value of x
// that minimizes the sum
static void findX(int []arr, int n)
{
    // Store the sum
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
 
    // Store the average of numbers
    int x = sum / n;
 
    int minSum = 0;
 
    // Find minimum sum
    for (int i = 0; i < n; i++) {
        minSum += (int)Math.Pow((arr[i] - x), 2);
    }
 
    Console.Write(minSum);
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 2, 9, 2, 6 };
    int n = arr.Length;
 
    findX(arr, n);
}
}
// This code is contributed by Samim Hossain Mondal.

Javascript



输出
46

时间复杂度:O(N)
辅助空间:O(1)