Open in App
Not now

# Minimize value of a given function for any possible value of X

• Difficulty Level : Easy
• Last Updated : 22 Jun, 2021

Given an array A[] consisting of N integers(1-based indexing), the task is to find the minimum value of the function  for any possible value of X.

Examples:

Input: A[] = {1, 2, 3, 4}
Output: 0
Explanation:
Consider the value of X as 0, then the value of the given function is (1 – 1 + 2 – 2 + 3 – 3 + 4 – 4) = 0, which is minimum.

Input: A[] = {5, 3, 9}
Output: 5

Approach: The given problem can be solved based on the following observations:

• Consider a function as (B[i] = A[i] − i), then to minimize the value of , the idea is to choose the value of X as the median of the array B[] such that the sum is minimized.

Follow the steps to solve the problem:

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach   #include  using namespace std;   // Function to find minimum value of // the given function int minimizeFunction(int A[], int N) {     // Stores the value of A[i] - i     int B[N];       // Traverse the given array A[]     for (int i = 0; i < N; i++) {           // Update the value of B[i]         B[i] = A[i] - i - 1;     }       // Sort the array B[]     sort(B, B + N);       // Calculate the median of the     // array B[]     int median = (B[int(floor((N - 1) / 2.0))]                   + B[int(ceil((N - 1) / 2.0))])                  / 2;       // Stores the minimum value of     // the function     int minValue = 0;       for (int i = 0; i < N; i++) {           // Update the minValue         minValue += abs(A[i] - (median + i + 1));     }       // Return the minimum value     return minValue; }   // Driver Code int main() {     int A[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };     int N = sizeof(A) / sizeof(A[0]);     cout << minimizeFunction(A, N);       return 0; }

## Java

 /*package whatever //do not write package name here */ import java.io.*; import java.lang.Math; import java.util.*;   class GFG {     public static int minimizeFunction(int A[], int N)     {                 // Stores the value of A[i] - i         int B[] = new int[N];           // Traverse the given array A[]         for (int i = 0; i < N; i++) {               // Update the value of B[i]             B[i] = A[i] - i - 1;         }           // Sort the array B[]         Arrays.sort(B);           // Calculate the median of the         // array B[]         int median = (B[(int)(Math.floor((N - 1) / 2.0))]                       + B[(int)(Math.ceil((N - 1) / 2.0))])                      / 2;           // Stores the minimum value of         // the function         int minValue = 0;           for (int i = 0; i < N; i++) {               // Update the minValue             minValue += Math.abs(A[i] - (median + i + 1));         }           // Return the minimum value         return minValue;     }       public static void main(String[] args)     {         int A[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };         int N = A.length;         System.out.println(minimizeFunction(A, N));     } } // This code is contributed by sam_2200.

## Python3

 # Python3 program for the above approach from math import floor, ceil   # Function to find minimum value of # the given function     def minimizeFunction(A, N):       # Stores the value of A[i] - i     B = [0] * N       # Traverse the given array A[]     for i in range(N):           # Update the value of B[i]         B[i] = A[i] - i - 1       # Sort the array B[]     B = sorted(B)       # Calculate the median of the     # array B[]     x, y = int(floor((N - 1) / 2.0)), int(ceil((N - 1) / 2.0))       median = (B[x] + B[y]) / 2       # Stores the minimum value of     # the function     minValue = 0       for i in range(N):           # Update the minValue         minValue += abs(A[i] - (median + i + 1))       # Return the minimum value     return int(minValue)     # Driver Code if __name__ == '__main__':       A = [1, 2, 3, 4, 5, 6, 7, 8, 9]     N = len(A)       print(minimizeFunction(A, N))   # This code is contributed by mohit kumar 29

## C#

 // C# program for the above approach using System;   class GFG {     public static int minimizeFunction(int[] A, int N)     {                 // Stores the value of A[i] - i         int[] B = new int[N];           // Traverse the given array A[]         for (int i = 0; i < N; i++) {               // Update the value of B[i]             B[i] = A[i] - i - 1;         }           // Sort the array B[]         Array.Sort(B);           // Calculate the median of the         // array B[]         int median = (B[(int)(Math.Floor((N - 1) / 2.0))] + B[(int)(Math.Ceiling((N - 1) / 2.0))])                      / 2;           // Stores the minimum value of         // the function         int minValue = 0;           for (int i = 0; i < N; i++) {               // Update the minValue             minValue += Math.Abs(A[i] - (median + i + 1));         }           // Return the minimum value         return minValue;     }       static void Main()     {         int []A = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };         int N = A.Length;         Console.WriteLine(minimizeFunction(A, N));     } } // This code is contributed by SoumikMondal.

## Javascript

 

Output:

0

Time Complexity: O(N * log N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles