Description
Solutions
Submission
Get Minimum Cost
Given an array of n positive integers, assuming 0-based indexing, its cost is
len(arr) is the size of the array.
Insert any integer at any location of the array such that the cost of the array is minimized. Find the minimum possible cost of the array after inserting exactly one element.
Function Description
Complete the function getMinimumCost
which has the following parameter:
int arr[n]
: an array of integersReturns
long_int
: the minimum possible cost of the array after insert one element.Example 1:
Input: arr = [1, 3, 5, 2, 10]
Output: 49
Explanation:The cost of the array before insertion = (1 - 3)^2 + (3 - 5)^2 + (5 - 2)^2 + (2 - 10)^2 = 81. Two of many scenarios are shown below. 1. Insert 4 between 3 and 5, cost of array = (1 - 3)^2 + (3 - 4)^2 + (4 - 5)^2 + (5 - 2)^2 + (2 - 10)^2 = 79. 2. Insert 6 between 2 and 10, cost of array = (1 - 3)^2 + (3 - 5)^2 + (5 - 2)^2 + (2 - 6)^2 + (6 - 10)^2 = 49. It can be proven that 49 is the minimum cost possible. Return 49.
Constraints:
2 <= n <= 104
1 <= arr[i] <= 105
Related Problems
Testcase
Result
Case 1
input:
output: