Problem · Array

Minimum Product Sum

Learn this problem
MediumTiktok logoTiktokNEW GRADOA
See Tiktok hiring insights

Problem statement

Given an array of n integers, arr[n], rearrange them so that the following equation is minimized:

sum(i = 1 to n - 1) of arr[i] * arr[i + 1]

Note: The equation uses 1-based indexing.

Complete the function findMinimumSum in the editor below.

findMinimumSum has the following parameter:

  • int arr[n]: the array to optimize

Return a long: the minimum sum of products from the array.

Function

findMinimumSum(arr: int[]) → long

Examples

Example 1

arr = [1, 10, 2, 7, 10, 6, 6]return = 127
Example 1 illustration

One of the optimal rearrangements is arr = [10, 1, 7, 6, 6, 2, 10], producing the result 10 * 1 + 1 * 7 + 7 * 6 + 6 * 6 + 6 * 2 + 2 * 10 = 127.

Constraints

  • 1 <= n <= 2 * 10^5
  • 1 <= arr[i] <= 10^5

More Tiktok problems

drafts saved locally
public long findMinimumSum(int[] arr) {
  // write your code here
}
arr[1, 10, 2, 7, 10, 6, 6]
expected127
checking account