Minimize value of a given function for any possible value of X
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: 0Explanation: 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++
#include <bits/stdc++.h>
using namespace std;
int minimizeFunction(int A[], int N)
{
int B[N];
for (int i = 0; i < N; i++) {
B[i] = A[i] - i - 1;
}
sort(B, B + N);
int median = (B[int(floor((N - 1) / 2.0))]
+ B[int(ceil((N - 1) / 2.0))])
/ 2;
int minValue = 0;
for (int i = 0; i < N; i++) {
minValue += abs(A[i] - (median + i + 1));
}
return minValue;
}
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
import java.io.*;
import java.lang.Math;
import java.util.*;
class GFG {
public static int minimizeFunction(int A[], int N)
{
int B[] = new int[N];
for (int i = 0; i < N; i++) {
B[i] = A[i] - i - 1;
}
Arrays.sort(B);
int median = (B[(int)(Math.floor((N - 1) / 2.0))]
+ B[(int)(Math.ceil((N - 1) / 2.0))])
/ 2;
int minValue = 0;
for (int i = 0; i < N; i++) {
minValue += Math.abs(A[i] - (median + i + 1));
}
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));
}
}
Python3
from math import floor, ceil
def minimizeFunction(A, N):
B = [0] * N
for i in range(N):
B[i] = A[i] - i - 1
B = sorted(B)
x, y = int(floor((N - 1) / 2.0)), int(ceil((N - 1) / 2.0))
median = (B[x] + B[y]) / 2
minValue = 0
for i in range(N):
minValue += abs(A[i] - (median + i + 1))
return int(minValue)
if __name__ == '__main__':
A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
N = len(A)
print(minimizeFunction(A, N))
C#
using System;
class GFG {
public static int minimizeFunction(int[] A, int N)
{
int[] B = new int[N];
for (int i = 0; i < N; i++) {
B[i] = A[i] - i - 1;
}
Array.Sort(B);
int median = (B[(int)(Math.Floor((N - 1) / 2.0))] + B[(int)(Math.Ceiling((N - 1) / 2.0))])
/ 2;
int minValue = 0;
for (int i = 0; i < N; i++) {
minValue += Math.Abs(A[i] - (median + i + 1));
}
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));
}
}
Javascript
<script>
function minimizeFunction(A, N){
let B = Array.from({length: N}, (_, i) => 0);
for (let i = 0; i < N; i++) {
B[i] = A[i] - i - 1;
}
B.sort();
let median = (B[(Math.floor((N - 1) / 2.0))]
+ B[(Math.ceil((N - 1) / 2.0))])
/ 2;
let minValue = 0;
for (let i = 0; i < N; i++) {
minValue += Math.abs(A[i] - (median + i + 1));
}
return minValue;
}
let A = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
let N = A.length;
document.write(minimizeFunction(A, N));
</script>
Time Complexity: O(N * log N)Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...