Find minimum value of the expression by choosing K elements from given array

Given an array of integers arr of size N, the task is to find the minimum possible of the expression by choosing exactly K(≤ N) integers form given array arr. Let say if choosen elements are stored in array B (B1, B2, B3…..Bk) then value of expression:
x = \sum_{i=1}^k\sum_{j=1}^k(B_i - B_j)^{2}

Examples:

Input : arr[] = {2, 0, 9, 5}, k = 2
Output : 8
Let say, choosen elements are {2, 0}, then x = 8, which is minimum possible

Input : arr[] = {4, 21, 5, 3, 8}, k = 3
Output : 200

Approach :
The above expression can be simplified as:



  • \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}

So, all we need to do is select the k smallest elements from the array and solve the expression.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find the minimum possible of the expression 
// by choosing exactly K(? N) integers form given array arr
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

200

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji, 29AjayKumar