Problem · Math
Minimum Possible Value of Function
Learn this problemProblem statement
You are given an integer sequence X of length N, X=( X[1], X[2], ..., X[N]). The function F is defined as follows:
F(k)= |X[1] - (k + 1)| + |X[2] - (k + 2)| +...+|X[N]-(k+N)|
Where k is an integer.
What is the minimum possible value of F(k) among all possible integers k?
NOTE: Here |x| denotes the absolute value of x.
Function
minimumPossibleValue(X: int[]) → long
Complete the function minimumPossibleValue in the editor.
minimumPossibleValue has the following parameter:
int[] X: an array of integers representing the sequence
Returns
long integer: the minimum possible value of F(k)
Examples
Example 1
X = [2, 2, 3, 5, 5]return = 2F(0) = |1| + |0| + |0| + |1| + |0| = 2.
Constraints
1 <= X[i] <= 10^91 <= N <= 2 * 10^5