1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

# Codewars 7 kyu Minimize Sum Of Array (Array Series #1)

Posted at

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/

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?