Given an n-dimensional array filled with numbers, output which p-dimensional array has the largest sum.
Input
- An n-dimensional array of numbers, or your language's equivalent. You can assume this is a rectangular array, or not.
- n, the number of dimensions in the array. (doesn't need to be called
n
) - p, the number of dimensions in the arrays you are adding up. (doesn't need to be called
p
)
Output
The respective indices of the p-dimensional array with the largest sum. You may have undefined behavior in the case of a tie. The dimensions of the array that you find should be the last p dimensions of the original array.
Test Cases
Input:
[[[4,2],
[6,8]],
[[0,0],
[500,327]]], 3, 2
Output:
1
Input:
[[1,2],
[4,3]], 2, 0
Since p=0, you're just finding the largest number in the array, which in this case is 4. The coordinates are:
1 0
Input:
[[1,2],
[4,-1]], 2, 1
You might see the 1 and 4 in the same column, and that that would have the largest sum of 5. However, because those share the second index, not the first, they are ignored. Both of the rows in this array add to 3, resulting in a tie, so you can return whatever you want here.
Input:
[[1,2],
[3,4]], 2, 2
Since p=n, there are no indices of the array with the largest sum, so the output would be empty.
Additional Rules
- Standard loopholes are forbidden.
- Output can be in any reasonable format.
- This is code-golf, so shortest answer in bytes wins.
[...]
around it. It might be worth clarifying exactly which dimensions we're adding. From the spec I would have assumed that we'd need to look at any subset of p dimensions, but judging by the last test case, we're only looking at adding up trailing dimensions (because I can add *p*=1 leading dimension and get 1+4 = 5). \$\endgroup\$n - p
? It's way better for some languages. \$\endgroup\$[[1,2], [4,-1]], 2, 1
returns either[1,2]
or[4,-1]
rather than[1,4]
?) \$\endgroup\$