Minimize value of x that minimizes value of |a1−x|^c+|a2−x|^c+···+|an−x|^c for value of c as 1 and 2
Last Updated :
24 Mar, 2023
Given an array arr[] of N elements, the task is to find the value of x that minimizes the value of expression for c = 1.
|a1−x|c+|a2−x|c+···+|an−x|c = |a1−x|+|a2−x|+···+|an−x|
Examples:
Input: arr[] = { 1, 2, 9, 2, 6 }
Output: 2
Explanation: The best solution is to select x = 2 which produces the sum |1−2| + |2−2| + |9−2| + |2−2| + |6−2| = 12 , which is the minimum possible sum, for all other values, the sum so obtained will be greater than 2
Input: arr[] = { 1, 2, 3, 4, 5 }
Output: 3
Approach: In the general case, the best choice for x is the median of the given numbers, The median is an optimal choice, because if x is smaller than the median, the sum becomes smaller by increasing x, and if x is larger than the median, the sum becomes smaller by decreasing x. Hence, the optimal solution is that x is the median.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findX( int arr[], int n)
{
sort(arr, arr + n);
int x;
if (n % 2 != 0) {
x = arr[n / 2];
}
else {
int a = arr[n / 2 - 1];
int b = arr[n / 2];
x = a;
}
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += abs (arr[i] - x);
}
cout << sum;
}
int main()
{
int arr1[] = { 1, 2, 9, 2, 6 };
int n1 = sizeof (arr1) / sizeof (arr1[0]);
findX(arr1, n1);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findX( int arr[], int n)
{
Arrays.sort(arr);
int x;
if (n % 2 != 0 ) {
x = arr[( int )Math.floor(n / 2 )];
}
else {
int a = arr[n / 2 - 1 ];
int b = arr[n / 2 ];
x = a;
}
int sum = 0 ;
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);
}
}
|
Python3
def findX(arr, n):
arr.sort();
x = None ;
if (n % 2 ! = 0 ):
x = arr[n / / 2 ];
else :
a = arr[(n / / 2 ) - 1 ];
b = arr[n / / 2 ];
x = a;
sum = 0 ;
for i in range (n):
sum + = abs (arr[i] - x);
print ( sum );
arr1 = [ 1 , 2 , 9 , 2 , 6 ];
n1 = len (arr1)
findX(arr1, n1);
|
C#
using System;
class GFG {
static void findX( int [] arr, int n)
{
Array.Sort(arr);
int x;
if (n % 2 != 0) {
x = arr[( int )Math.Floor(( float )(n / 2))];
}
else {
int a = arr[n / 2 - 1];
x = a;
}
int sum = 0;
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);
}
}
|
Javascript
<script>
function findX(arr, n) {
arr.sort((a, b) => a - b);
let x;
if (n % 2 != 0) {
x = arr[(Math.floor(n / 2))];
}
else {
let a = arr[(Math.floor(n / 2) - 1)];
let b = arr[(Math.floor(n / 2))];
x = a;
}
let sum = 0;
for (let i = 0; i < n; i++) {
sum += Math.abs(arr[i] - x);
}
document.write(sum);
}
let arr1 = [1, 2, 9, 2, 6];
let n1 = arr1.length;
findX(arr1, n1);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Given an array arr[] of N elements, the task is to find the value of x that minimizes the value of expression for c = 2.
|a1−x|c+|a2−x|c+···+|an−x|c = (a1−x)2+(a2−x)2+···+(an−x)2.
Examples :
Input: arr[] = { 1, 2, 9, 2, 6 }
Output: 4
Explanation: The best solution is to select x = 4 which produces the sum (1−4)^2 + (2−4)^2 + (9−4)^2 + (2−4)^2 + (6−4)^2 = 46, which is the minimum possible sum.
Input: arr[] = { 1, 2, 2, 4, 6 }
Output: 3
Approach: In the general case, the best choice for x is the average of the numbers. This result can be derived by expanding the sum as follows:
nx2−2x(a1+a2+···+an) + (a12+a22+···+an2)
The last part does not depend on x. The remaining parts form a function nx2 − 2xs where s=a1+a2+···+an. Applying derivative to this equation w.r.t x and equating the result to zero gives us x = s / n, which is the value that minimizes the sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findX( int arr[], int n)
{
double sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
}
double x = sum / n;
double minSum = 0;
for ( int i = 0; i < n; i++) {
minSum += pow ((arr[i] - x), 2);
}
cout << minSum;
}
int main()
{
int arr[] = { 1, 2, 9, 2, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
findX(arr, n);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static void findX( int []arr, int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
sum += arr[i];
}
int x = sum / n;
int minSum = 0 ;
for ( int i = 0 ; i < n; i++) {
minSum += Math.pow((arr[i] - x), 2 );
}
System.out.print(minSum);
}
public static void main(String args[])
{
int []arr = { 1 , 2 , 9 , 2 , 6 };
int n = arr.length;
findX(arr, n);
}
}
|
Python3
def findX(arr, n):
sum = 0 ;
for i in range (n):
sum + = arr[i];
x = sum / / n;
minSum = 0 ;
for i in range (n):
minSum + = pow ((arr[i] - x), 2 );
print (minSum);
if __name__ = = '__main__' :
arr = [ 1 , 2 , 9 , 2 , 6 ];
n = len (arr);
findX(arr, n);
|
C#
using System;
class GFG
{
static void findX( int []arr, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
}
int x = sum / n;
int minSum = 0;
for ( int i = 0; i < n; i++) {
minSum += ( int )Math.Pow((arr[i] - x), 2);
}
Console.Write(minSum);
}
public static void Main()
{
int []arr = { 1, 2, 9, 2, 6 };
int n = arr.Length;
findX(arr, n);
}
}
|
Javascript
<script>
function findX(arr, n)
{
let sum = 0;
for (let i = 0; i < n; i++) {
sum += arr[i];
}
let x = sum / n;
let minSum = 0;
for (let i = 0; i < n; i++) {
minSum += Math.pow((arr[i] - x), 2);
}
document.write(minSum);
}
let arr = [ 1, 2, 9, 2, 6 ];
let n = arr.length;
findX(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...