Find minimum value of the expression by choosing K elements from given array | C # 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 ; Sorting the array for least k element selection ; Select first k elements from sorted array ; Return value of solved expression ; Driver code ; Function call
using System ; class GFG { static int minimumValue ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int answer = 0 ; for ( int i = 0 ; i < k ; i ++ ) answer += arr [ i ] * arr [ i ] ; return answer * ( 2 * k - 2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 21 , 5 , 3 , 8 } ; int k = 3 ; int n = arr . Length ; Console . Write ( minimumValue ( arr , n , k ) ) ; } }