Problem · Math

Minimum Possible Value of Function

Learn this problem
MediumWalmartOA

Problem 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:

  1. 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 = 2
F(0) = |1| + |0| + |0| + |1| + |0| = 2.

Constraints

  • 1 <= X[i] <= 10^9
  • 1 <= N <= 2 * 10^5

More Walmart problems

drafts saved locally
public long minimumPossibleValue(int[] X) {
    // write your code here
}
X[2, 2, 3, 5, 5]
expected2
checking account