Codewars 7 kyu Minimize Sum Of Array (Array Series #1)
Task
Given an array of integers , Find the minimum sum which is obtained from summing each Two integers product.
Verbalization
Sort the arrays
Pair the elements of smallest and largest
= divide sorted arrays by 2 and sort reverse the large one.
Calculate the each sum
#Code
import numpy
def min_sum(arr):
a = numpy.array(sorted(arr))
a0, a1 = numpy.array_split(a, 2) # safe even for odd-length arrays
a1 = a1[::-1] # reverse second half
return int(numpy.sum(a0 * a1)) # element-wise multiply and sum
Reference
https://note.nkmk.me/python-numpy-split/#numpysplit
https://deepage.net/features/numpy-sum.html
nympy配列とpythonリストの変換:https://note.nkmk.me/python-numpy-list/