Minimum Possible Value of Function
Updated
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:
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
More Walmart interview questions
- Merge a List In Between Two PositionsONSITE INTERVIEW · Seen Nov 2026
- Merge Intervals from Start and End ListsONSITE INTERVIEW · Seen Nov 2026
- Palindrome Check Ignoring SymbolsONSITE INTERVIEW · Seen Nov 2026
- Largest Team Where Every Pair OverlapsOA · Seen Nov 2026
- House RobberOA · Seen Sep 2026
- Maximum Points by Deleting ElementsOA · Seen Sep 2026
- Distinct Bitwise-OR Scores of Increasing SubsequencesOA · Seen Sep 2026
- Best Meeting PointOA · Seen Sep 2026
- Minimum Cost to Make Array EqualOA · Seen Sep 2026
- Minimum Stress PathOA · Seen Aug 2026
Similar questions at other companies
- Count Distinct Arrays After Decrement OperationsBloomberg LP · OABloomberg LP interview questions
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Prepare Taxi Driver Classification DataBoston Consulting Group · OABoston Consulting Group interview questions
- Macaron Order PriceOpenAI · OAOpenAI interview questions
- Linear InterpolatorTwo Sigma · OATwo Sigma interview questions
- Return Exact Cash Register ChangeBank of America · OABank of America interview questions
- Product of Subset MaximaArcesium · OAArcesium interview questions
- REST API: Energy BarRippling · OARippling interview questions
- Paint the CeilingSnowflake · OASnowflake interview questions