You are given two independent coding tasks.
Task 1: Minimum total cost to reduce an array
You are given an integer array arr of length n.
You may repeatedly perform the following reduction operation until only one number remains:
-
Choose any two elements
x
and
y
currently in the array.
-
Remove both from the array.
-
Insert their sum
x + y
back into the array.
-
The
cost
of this operation is
x + y
.
Return the minimum possible total cost over all sequences of operations.
Input: arr (integers)
Output: minimum total cost (use 64-bit integer)
Notes/assumptions (make explicit in your solution):
-
n >= 1
.
-
Elements can be any integers; total cost should be computed using 64-bit arithmetic.
Task 2: Pair elements into equal-sum groups and sum products
You are given an integer array arr of even length 2m.
You must partition the array into m disjoint pairs such that:
-
Every element is used in exactly one pair.
-
The
sum of the two numbers in each pair is the same constant
S
for all pairs.
If such a partition is possible, return:
-
The value
∑i=1m(ai×bi)
, i.e., the
sum of products
of each pair.
If it is not possible to form such pairs, return -1.
Input: arr (even length)
Output: sum of pairwise products, or -1 (use 64-bit integer)