R, 38 bytes
function(x)sum(combn(c(0,x),2,diff)<0)
Try it online!
Adaptation of ovs's answer. Test harness taken from Dominic van Essen's answer.
combn(x,m)
in R generates all size m
combinations of the elements of x
1, and has an optional FUN
argument which it applies to each of those combinations. As luck would have it, it's implemented in such a way as to maintain the order of elements in x
in each of those combinations; for x=c(3,1,4,2)
:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 3 3 1 1 4
[2,] 1 4 2 4 2 2
Using FUN=diff
results in a vector which contains the upper-triangular part of the matrix of comparisons:
[1] -2 1 -1 3 1 -2
These are negative precisely where there is a swapped pair, so count the negatives.
Unfortunately, R will throw an error for combn(1,2)
since it can't get a combination of 2 out of a single value, so 0
is prepended. This has no impact (since the diff
will always be positive), and neatly fixes that edge case.
1 If x
is a single numeric, it instead uses seq_len(n)
which will be 1:floor(n)
for n>=1
and an empty vector otherwise. R functions sometimes have weird special cases for length-one inputs.