This two-part question evaluates array manipulation and algorithmic problem-solving skills: the first task focuses on optimizing cumulative operation costs during iterative reductions while the second assesses combinatorial pairing under equal-sum constraints and computation of aggregate pairwise products.
You are given two independent coding tasks.
You are given an integer array arr of length n.
You may repeatedly perform the following reduction operation until only one number remains:
x
and
y
currently in the array.
x + y
back into the array.
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
.
You are given an integer array arr of even length 2m.
You must partition the array into m disjoint pairs such that:
S
for all pairs.
If such a partition is possible, return:
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)