📌  相关文章
📜  通过从给定数组中选择K个元素来找到表达式的最小值

📅  最后修改于: 2021-04-27 05:27:02             🧑  作者: Mango

给定大小为N的整数arr数组,任务是通过从给定数组arr中精确选择K(≤N)个整数来找到表达式的最小值。假设如果选择的元素存储在数组B (B 1 ,B 2 ,B 3 …..B k )中,则表达式的值为:
x = \sum_{i=1}^k\sum_{j=1}^k(B_i - B_j)^{2}
例子:

方法 :
上面的表达式可以简化为:

  • \sum_{i=1}^k\sum_{j=1}^k(B_i^2 + B_j^2 - 2*B_i*B_j)
  • \sum_{i=1}^k\sum_{j=1}^k{B_i^2} + \sum_{i=1}^k\sum_{j=1}^k{B_j^2} - 2\sum_{i=1}^k\sum_{j=1}^k{B_i*B_j}
  • k*\sum_{i=1}^k{B_i^2} + k*\sum_{i=1}^k{B_j^2} - 2\sum_{i=1}^k{B_i}*\sum_{j=1}^k{B_j}
  • 2*k*\sum_{i=1}^k{B_i^2} - 2\sum_{i=1}^k{B_i^2}
  • (2*k-2)*\sum_{i=1}^k{B_i^2}

因此,我们要做的就是从数组中选择k个最小的元素并求解表达式。
下面是上述方法的实现:

C++
// CPP program to find the minimum possible of the expression
// by choosing exactly K(? N) integers form given array arr
#include 
using namespace std;
 
// Function to find the minimum possible of the expression
// by choosing exactly K(? N) integers form given array arr
int minimumValue(int arr[], int n, int k)
{
 
    // Sorting the array for least k element selection
    sort(arr, arr + n);
 
    int answer = 0;
 
    // Select first k elements from sorted array
    for (int i = 0; i < k; i++)
        answer += arr[i] * arr[i];
 
    // Return value of solved expression
    return answer * (2 * k - 2);
}
 
// Driver code
int main()
{
    int arr[] = { 4, 21, 5, 3, 8 }, k = 3;
     
    int n = sizeof(arr) / sizeof(arr[0]);
     
    // Function call
    cout << minimumValue(arr, n, k);
     
    return 0;
}


Java
// JAVA program to find the minimum possible of the expression
// by choosing exactly K(? N) integers form given array arr 
import java.util.*;
 
class GFG{
 
// Function to find the minimum possible of the expression
// by choosing exactly K(? N) integers form given array arr
static int minimumValue(int arr[], int n, int k)
{
 
    // Sorting the array for least k element selection
    Arrays.sort(arr);
 
    int answer = 0;
 
    // Select first k elements from sorted array
    for (int i = 0; i < k; i++)
        answer += arr[i] * arr[i];
 
    // Return value of solved expression
    return answer * (2 * k - 2);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 21, 5, 3, 8 }, k = 3;
     
    int n = arr.length;
     
    // Function call
    System.out.print(minimumValue(arr, n, k));    
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python program to find the minimum
# possible of the expression by choosing
# exactly K(? N) integers form given array arr
 
# Function to find the minimum
# possible of the expression by
# choosing exactly K(? N) integers
# form given array arr
def minimumValue(arr, n, k):
 
    # Sorting the array for least k element selection
    arr.sort();
 
    answer = 0;
 
    # Select first k elements from sorted array
    for i in range(k):
        answer += arr[i] * arr[i];
 
    # Return value of solved expression
    return answer * (2 * k - 2);
 
# Driver code
if __name__ == '__main__':
    arr = [ 4, 21, 5, 3, 8 ];
    k = 3;
 
    n = len(arr);
 
    # Function call
    print(minimumValue(arr, n, k));
 
# This code is contributed by Rajput-Ji


C#
// C# program to find the minimum possible of the expression
// by choosing exactly K(? N) integers form given array arr 
using System;
 
class GFG{
  
// Function to find the minimum possible of the expression
// by choosing exactly K(? N) integers form given array arr
static int minimumValue(int []arr, int n, int k)
{
  
    // Sorting the array for least k element selection
    Array.Sort(arr);
  
    int answer = 0;
  
    // Select first k elements from sorted array
    for (int i = 0; i < k; i++)
        answer += arr[i] * arr[i];
  
    // Return value of solved expression
    return answer * (2 * k - 2);
}
  
// Driver code
public static void Main(String[] args)
{
    int []arr = { 4, 21, 5, 3, 8 };
    int k = 3;
      
    int n = arr.Length;
      
    // Function call
    Console.Write(minimumValue(arr, n, k));    
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
200