Sunday, October 12, 2025
HomeData Modelling & AIFind the minimum value of X for an expression

Find the minimum value of X for an expression

Given an array arr[]. The task is t find the value of X such that the result of the expression (A[1] – X)^2 + (A[2] – X)^2 + (A[3] – X)^2 + … (A[n-1] – X)^2 + (A[n] – X)^2 is minimum possible.
Examples : 
 

Input : arr[] = {6, 9, 1, 6, 1, 3, 7} 
Output : 5
Input : arr[] = {1, 2, 3, 4, 5} 
Output :
 

 

Approach: 
We can simplify the expression that we need to minimize. The expression can be written as 

(A[1]^2 + A[2]^2 + A[3]^2 + … + A[n]^2) + nX^2 – 2X(A[1] + A[2] + A[3] + … + A[n])

On differentiating the above expression, we get 

 2nX - 2(A[1] + A[2] + A[3] + … + A[n])

We can denote the term (A[1] + A[2] + A[3] + … + A[n] ) as S. We get 

 2nX - 2S 

Putting 2nX – 2S = 0, we get 

 X = S/N 

Below is the implementation of the above approach: 
 

C++




// C++ implementation of above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate value of X
int valueofX(int ar[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum = sum + ar[i];
    }
 
    if (sum % n == 0) {
        return sum / n;
    }
    else {
        int A = sum / n, B = sum / n + 1;
        int ValueA = 0, ValueB = 0;
 
        // Check for both possibilities
        for (int i = 0; i < n; i++) {
            ValueA += (ar[i] - A) * (ar[i] - A);
            ValueB += (ar[i] - B) * (ar[i] - B);
        }
 
        if (ValueA < ValueB) {
            return A;
        }
        else {
            return B;
        }
    }
}
 
// Driver Code
int main()
{
    int n = 7;
    int arr[7] = { 6, 9, 1, 6, 1, 3, 7 };
 
    cout << valueofX(arr, n) << '\n';
 
    return 0;
}


Java




// Java implementation of above approach
class GFG
{
 
// Function to calculate value of X
static int valueofX(int ar[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        sum = sum + ar[i];
    }
 
    if (sum % n == 0)
    {
        return sum / n;
    }
    else
    {
        int A = sum / n, B = sum / n + 1;
        int ValueA = 0, ValueB = 0;
 
        // Check for both possibilities
        for (int i = 0; i < n; i++)
        {
            ValueA += (ar[i] - A) * (ar[i] - A);
            ValueB += (ar[i] - B) * (ar[i] - B);
        }
 
        if (ValueA < ValueB)
        {
            return A;
        }
        else
        {
            return B;
        }
    }
}
 
// Driver Code
public static void main(String args[])
{
    int n = 7;
    int arr[] = { 6, 9, 1, 6, 1, 3, 7 };
 
    System.out.println(valueofX(arr, n));
}
}
 
// This code is contributed by PrinciRaj1992


Python3




# Python3 implementation of above approach
 
# Function to calculate value of X
def valueofX(ar, n):
    summ = sum(ar)
 
    if (summ % n == 0):
        return summ // n
    else:
        A = summ // n
        B = summ // n + 1
        ValueA = 0
        ValueB = 0
 
        # Check for both possibilities
        for i in range(n):
            ValueA += (ar[i] - A) * (ar[i] - A)
            ValueB += (ar[i] - B) * (ar[i] - B)
 
        if (ValueA < ValueB):
            return A
        else:
            return B
 
# Driver Code
n = 7
arr = [6, 9, 1, 6, 1, 3, 7]
 
print(valueofX(arr, n))
 
# This code is contributed by Mohit Kumar


C#




// C# implementation of above approach
using System;
                     
class GFG
{
 
// Function to calculate value of X
static int valueofX(int []ar, int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        sum = sum + ar[i];
    }
 
    if (sum % n == 0)
    {
        return sum / n;
    }
    else
    {
        int A = sum / n, B = sum / n + 1;
        int ValueA = 0, ValueB = 0;
 
        // Check for both possibilities
        for (int i = 0; i < n; i++)
        {
            ValueA += (ar[i] - A) * (ar[i] - A);
            ValueB += (ar[i] - B) * (ar[i] - B);
        }
 
        if (ValueA < ValueB)
        {
            return A;
        }
        else
        {
            return B;
        }
    }
}
 
// Driver Code
public static void Main(String []args)
{
    int n = 7;
    int []arr = { 6, 9, 1, 6, 1, 3, 7 };
 
    Console.WriteLine(valueofX(arr, n));
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
// JavaScript implementation of above approach
 
// Function to calculate value of X
function valueofX(ar, n)
{
    let sum = 0;
    for (let i = 0; i < n; i++) {
        sum = sum + ar[i];
    }
 
    if (sum % n == 0) {
        return Math.floor(sum / n);
    }
    else {
        let A = Math.floor(sum / n), B = Math.floor(sum / n + 1);
        let ValueA = 0, ValueB = 0;
 
        // Check for both possibilities
        for (let i = 0; i < n; i++) {
            ValueA += (ar[i] - A) * (ar[i] - A);
            ValueB += (ar[i] - B) * (ar[i] - B);
        }
 
        if (ValueA < ValueB) {
            return A;
        }
        else {
            return B;
        }
    }
}
 
// Driver Code
    let n = 7;
    let arr = [ 6, 9, 1, 6, 1, 3, 7 ];
 
    document.write(valueofX(arr, n) + "<br>");
 
// This code is contributed by Surbhi Tyagi.
</script>


Output: 

5

 

Time Complexity: O(n)

Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32352 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11885 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6840 POSTS0 COMMENTS
Ted Musemwa
7105 POSTS0 COMMENTS
Thapelo Manthata
6796 POSTS0 COMMENTS
Umr Jansen
6795 POSTS0 COMMENTS