This was originally a question from interviewstreet.com over a decade ago
You are given an array (or whatever - your choice of input) of positive integers \$y_1,\ldots,y_n\$ that represents \$n\$ line segments, where segment \$i\$ has endpoints \$(i, 0)\$ and \$(i, y_i)\$. Imagine that from the top of each segment a horizontal ray is shot to the left, and this ray stops when it touches another segment or it hits the \$y\$-axis. Construct an array of \$n\$ integers, \$v_1,\ldots,v_n\$, where \$v_i\$ is equal to the length of ray shot from the top of segment \$i\$. Define \$V(y_1,\ldots,y_n)=v_1+\cdots+v_n\$.
For example starting with \$3,2,5,3,3,4,1,2\$, then \$v_1,\ldots,v_8=1,1,3,1,1,3,1,2\$ as shown in this picture, and so \$V(3,2,5,3,3,4,1,2)=1+1+3+1+1+3+1+2=13\$ here.

For each permutation \$p\$ of \$1,\ldots,n\$, you can calculate \$V(y_{p_1},\ldots,y_{p_n})\$. You can then take the average across all possible permutations.
Write a program or function that gives the expected value of \$V(y_{p_1},\ldots,y_{p_n})\$ for a uniformly random permutation \$p\$ of \$1,\ldots,n\$. Include a complexity measure for your approach, such as \$O(n)\$ or \$O(n!)\$ and explain why.
This is code-golf, so the shortest answer in bytes wins. Standard rules apply and default loopholes are forbidden.
Test examples include
Input y Output E[V]
3,2,5,3,3,4,1,2 15.25
1,2,3 4.333333
3,3,3 3
2,2,3 4
10,2,4,4 6
10,10,10,5,10 5.8
1,2,3,4,5,6 11.15
code-golfmost answers are likely to use the worst possible time complexity. (Or even worse than the worst, if that saves a byte.) \$\endgroup\$