Problem · Array
Get Minimum Cost
Learn this problemProblem statement
For an integer array arr, define its cost as:
Σ (arr[x] - arr[x + 1])^2 for every index 0 ≤ x < arr.length - 1.
Insert exactly one integer at any position in the array. Return the minimum possible cost after the insertion.
Function
getMinimumCost(arr: int[]) → longExamples
Example 1
arr = [1, 3, 5, 2, 10]return = 49The original cost is 81. Insert 6 between 2 and 10. That edge contributes (2 - 6)^2 + (6 - 10)^2 = 32 instead of (2 - 10)^2 = 64, so the total becomes 49.
Constraints
2 ≤ arr.length ≤ 10^41 ≤ arr[i] ≤ 10^5
More IBM problems
- Parent Process NumberOA · Seen Jul 2026
- Request Retry CountOA · Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA · Seen Jul 2026
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Spam Text ClassificationOA · Seen Jul 2026
- Count Ideal NumbersOA · Seen Jun 2026