| Bytes | Lang | Time | Link |
|---|---|---|---|
| 019 | Uiua + experimentals | 240730T002210Z | noodle p |
| 086 | Haskell | 240801T180357Z | MaroonSp |
| 030 | J | 240720T055517Z | noodle p |
| 191 | Setanta | 240720T212816Z | bb94 |
| 093 | Python 3 | 240718T071207Z | Jitse |
| 099 | JavaScript ES6 | 240718T064538Z | Arnauld |
| 104 | R | 240718T151236Z | Dominic |
| 330 | Elisp | 240718T150721Z | Samuel J |
| 041 | Raku | 240718T132936Z | Ven |
| 136 | R | 240718T061307Z | pajonk |
| 016 | Haskell + hgl | 240718T115820Z | Wheat Wi |
| 008 | Jelly | 240718T104737Z | Jonathan |
| 051 | Charcoal | 240718T083302Z | Neil |
| 008 | 05AB1E | 240718T065430Z | Kevin Cr |
| 058 | Arturo | 240718T031557Z | chunes |
| 055 | Factor + math.combinatorics | 240718T020251Z | chunes |
| 006 | Vyxal | 240718T013914Z | emanresu |
Uiua + experimentals, 19 bytes
⊡⊢⍏/↥⌵\+⍉.⧅≠⊸⧻
⊡⊢⍏/↥⌵\+⍉.⧅≠⊸⧻
⊡⊢⍏ minimum by
/↥ maximum
⌵ absolute
\+ prefix sum
⍉. of each
⧅≠⊸⧻ permutation of the input
Haskell, 86 bytes
import Data.List
h=maximum.map abs.scanl1(+)
f=minimumBy((.h).compare.h).permutations
Equivalent, more readable version:
import Data.List
partialSums = scanl1 (+)
helper = maximum . map abs . partialSums
answer = minimumBy (\left right -> compare (helper left) (helper right)) . permutations
J, 37 31 30 bytes
0{[:{{y/:>./|+/\|:y}}i.@!@#A.]
i.@!@#A.] NB. get the permutations:
i. NB. the range to
@! NB. the factorial of
@# NB. the length of the argument
A.] NB. select those permutations of the argument
0{[:{{y/:>./|+/\|:y}} NB. reorder for smallest largest prefix sum:
0{[: NB. first of
{{y/: |:y}} NB. sorting those by each's
>./ NB. maximum
| NB. absolute
+/\ NB. prefix sum
Setanta, 225 193 192 191 bytes
Another brute-force solution, but in a language without a permutation builtin.
gniomh(l){s:=eas@mata(999)p:=0gniomh f(v,w,n,m){ma fad@v<fad@l le i idir(0,fad@l){ma aimsigh@v(i)<0{N:=l[i]f(v+[i],w+[N],n+N,uas(dearbh@mata(n+N),m))}}no ma m<s{p=w s=m}}f([],[],0,0)toradh p}
Python 3, 93 bytes
lambda a:min(permutations(a),key=lambda a:max(map(abs,accumulate(a))))
from itertools import*
-45 bytes thanks to Jonathan Allan
-3 bytes thanks to no comment
Python 3.8 (pre-release), 104 bytes
f=lambda a:a and min((f((a:=a[1:]+[x])[:-1])+[x]for x in a),key=lambda a,i=0:max(abs(i:=i+x)for x in a))
Solution without itertools
JavaScript (ES6), 99 bytes
Naive method trying all possible permutations.
f=(a,s=M=f,m,...p)=>m>M||a.map((v,i)=>f(a.filter(_=>i--),q=~~s+v,q*q<m?m:q*q,...p,v))+a?o:(M=m,o=p)
Commented
f = ( // f is a recursive function taking:
a, // a[] = input array
s = // s = current sum (initially NaN'ish)
M = f, // M = minimum of maximums (initially NaN'ish)
m, // m = maximum of squared cumulative sums
// (initially undefined)
...p // p[] = current permutation
) => //
m > M || // abort if m is greater than M
a.map((v, i) => // otherwise, for each value v at index i in a[]:
f( // do a recursive call:
a.filter(_ => // pass a[] ...
i-- // ... without the i-th element
), // end of filter()
q = ~~s + v, // define q as s + v
q * q < m ? // if q² is less than m:
m // leave m unchanged
: // else:
q * q, // set m to q²
...p, // append to p[] ...
v // ... the new element v
) // end of recursive call
) + a // end of map()
? // if we had m > M or a[] is not empty:
o // just return o[]
: // else:
(M = m, o = p) // update M to m and o[] to p[]
R, 124 108 104 bytes
Edit: -13 bytes thanks to Giuseppe.
Edit2: -4 bytes thanks to pajonk
\(x,n=sum(x|1),m=combn(rep(1:n,n),n))x[m[,order(apply(m,2,\(v)max(cumsum(x[v])^2)/all(table(v)<2)))[1]]]
How?
- Get all combinations
vof all indices of inputx(including re-use of each index, since there isn't an easy base-R command to get permutations). - Calculate
max(abs(cumsum(x[v])))for each. - Divide by
all(table(v)<2))): this is zero for combinations with re-used indices, givingInfafter division. - Get the minimum, and select the elements of the input using the corresponding indices.
Elisp, 341 330 Bytes
Edit: learned the sort function
(defun x (bag)(if (null bag)'(())(mapcan #'(lambda (e)(mapcar #'(lambda (p) (cons e p))(x(cl-remove e bag :count 1))))bag)))(defun z (a)(car (car (sort(cl-loop for e in (x a) collect(let ((i (cons e (apply 'max(cl-loop for o = (reverse e) then (cdr o) while o collect(abs (apply '+ o)))))))i))'(lambda (j k)(< (cdr j) (cdr k))))))
Used permutation method from here https://lists.gnu.org/archive/html/help-gnu-emacs/2009-06/msg00094.html
(defun permutations (bag)
(if (null bag)
'(())
(mapcan #'(lambda (e)
(mapcar #'(lambda (p) (cons e p))
(permutations
(cl-remove e bag :count 1))))
bag)))
(defun smallest-largest-prefix-sum (a)
(car (car (sort
(cl-loop for e in (permutations a) collect
(let ((i (cons e (apply 'max
(cl-loop for o = (reverse e) then (cdr o) while o collect
(abs (apply '+ o))
)
)
)))i))
'(lambda (j k)
(< (cdr j) (cdr k))))
)))
Raku, 41 bytes
*.permutations.min:{([\+] @$_)>>.abs.max}
[\+] is the cumulative sum (+\ in APL).
>>. acts like .map{} (in theory, it might be parallelized, but its results are guaranteed to be in order).
R, 136 bytes
\(x)(~x)[order(apply(~x,1,\(v)max(abs(cumsum(v)))))[1],]
`~`=\(v)`if`(n<-sum(v|1),{X={};for(i in 1:n)X=rbind(X,cbind(v[i],~v[-i]));X},v)
Permutations in base R are a pain. Code for permutations adapted from here.
Haskell + hgl, 16 bytes
mB(xM av<scp)<pm
Explanation
pmget all permutationsmBminimum by ...scpcumulative sumsxM avmaximum absolute value
Reflection
This is a fairly straight forward solution. I don't see any potential other ways to do this. Nor do I see any ways in which two or more of the components here might be combined to make this shorter.
Jelly, 8 bytes
Œ!ÄAṀƊÞḢ
A monadic Link that accepts a list of integers and yields one with the minimal maximum absolute prefix sum.
Try it online! Or see the test-suite.
How?
Œ!ÄAṀƊÞḢ - Link: list of integers, A
Œ! - all permutations of A
Þ - sort by:
Ɗ - last three links as a monad:
Ä - cumulative sums
A - absolute value (vectorises)
Ṁ - maximum
Ḣ - head
Charcoal, 51 bytes
⊞υ⟦⟧Fθ≔ΣEυE⁻Eθνκ⁺κ⟦μ⟧υFυUMι§θκ≔Eυ⌈↔EιΣ…ι⊕μη⭆¹§υ⌕η⌊η
Attempt This Online! Link is to verbose version of code. Explanation:
⊞υ⟦⟧Fθ≔ΣEυE⁻Eθνκ⁺κ⟦μ⟧υ
Generate all of the permutations of the indices of the input. This is taken from my answer to Count N-Rich Permutations of an Integer Sequence. I use the indices because they're unique and it makes it easier to generate the permutations.
FυUMι§θκ
Map each index to the actual value from the input list.
≔Eυ⌈↔EιΣ…ι⊕μη
Calculate the maximum prefix sum for each permutation.
⭆¹§υ⌕η⌊η
Output the permutation with the minimum sum.
05AB1E, 8 bytes
œΣηOÄà}н
Try it online or verify all test cases
or verify all possible outputs (by using an additional group-by after the sort).
Explanation:
œ # Get all permutations of the (implicit) input-list
Σ # Sort this list of permutation-lists by:
ηO # Cumulative sum:
η # Get the prefixes
O # Sum each prefix together
Ä # Take the absolute value of each sum
à # Pop and leave the maximum of those
}н # After the sort by: take the first/smallest
# (which is output implicitly as result)
Arturo, 58 bytes
$=>[minimum permutate&'p->max map size p=>[abs∑take p&]]
Explanation
$=>[] ; a function where input is assigned to &
minimum permutate&'p-> ; minimum permutation of input by; current perm is p
max ; max value in
map size p=>[] ; map over current perm's indices, assign to &
abs ; absolute value
∑ ; sum
take p& ; current prefix of current permutation
Factor + math.combinatorics, 55 bytes
[ <permutations> [ cum-sum vabs supremum ] infimum-by ]
<permutations> [ ... ] infimum-byGet the smallest permutation by function[ ... ]. The angle brackets aren't special; it's just a naming convention signifying that it's a virtual sequence (similar to a lazy list).cum-sumcumulative sumvabsvectorized absolute valuesupremumlargest
Vyxal, 6 bytes
Ṗ≬¦ȧG∵
∵ # Smallest
Ṗ # Permutation
≬--- # By
G # Largest
ȧ # Absolute
¦ # Cumulative sum