← Back to challenges

Column With Maximum Sum

PythonHardarrayslanguage_fundamentals

Instructions

Given a list of numbers and a value for n, split the numbers into n-sized groups. If we imagine that these groups are stacked on top of each other (see below), return the column number that has the greatest sum. If two or more columns have the same sum, return the one with the smallest column number.

Example

nums = [4, 14, 12, 7, 14, 16, 5, 13, 7, 16, 11, 19]
n = 4

Gives the array:

[[4, 14, 12, 7],
[14, 16,  5, 13],
[7, 16, 11, 19]]

# 1, 2, 3, 4 (column)
# 25, 46, 28, 39 (sum)

You would return 2, as the 2nd column has the greatest sum.

Notes

Nums will always divide into equal-length groups.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.