[Question]
  [
As computer scientists, you're probably all familiar with the basic list operations of *pop* and *push*. These are simple operations that modify a list of elements. However, have you ever heard of the operation *flop*? (as in *flip-**flop***)? It's pretty simple. Given a number **n**, reverse the first **n** elements of the list. Here's an example:
```
>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a.flop(4)
[4, 3, 2, 1, 5, 6, 7, 8, 9, 10]
```
The cool thing about the flop operation, is that you can use it do some cool things to a list, such as [sorting it](https://en.wikipedia.org/wiki/Pancake_sorting). We're going to do something similar with flops:
> 
> Given a list of integers, "Neighbor it". In other words, sort it so that every duplicate element appears consecutively.
> 
> 
> 
This can be done with flops! For example, take the following list:
```
>>> a = [3, 2, 1, 4, 3, 3, 2]
>>> a.flop(4)
[4, 1, 2, 3, 3, 3, 2]
>>> a.flop(3)
[2, 1, 4, 3, 3, 3, 2]
>>> a.flop(6)
[3, 3, 3, 4, 1, 2, 2]
```
This leads us to the definition of today's challenge:
> 
> Given a list of integers, output any set of flops that will result in the list being neighbored.
> 
> 
> 
Using the last list as an example, you should output:
```
4
3
6
```
because flopping the list by 4, then by 3, then by 6 will result in a neighbored list. Keep in mind that you do *not* need to print the shortest possible list of flops that neighbors a list. If you had printed:
```
4
4
4
3
1
1
6
2
2
```
instead, this would still be a valid output. However, you may *not* ever output a number larger than the length of the list. This is because for a list `a = [1, 2, 3]`, calling `a.flop(4)` is nonsensical.
Here are some examples:
```
#Input:
[2, 6, 0, 3, 1, 5, 5, 0, 5, 1]
#Output
[3, 7, 8, 6, 9]
#Input
[1, 2]
#Output
<any list of integers under 3, including an empty list>
#Input
[2, 6, 0, 2, 1, 4, 5, 1, 3, 2, 1, 5, 6, 4, 4, 1, 4, 6, 6, 0]
#Output
[3, 19, 17, 7, 2, 4, 11, 15, 2, 7, 13, 4, 14, 2]
#Input
[1, 1, 1, 1, 2, 2, 2, -1, 4]
#Output
[]
#Input
[4, 4, 8, 8, 15, 16, 16, 23, 23, 42, 42, 15]
#Output
[12, 7]
```
Keep in mind that in each of these examples, the output given is just one potential valid output. As I said before, *any set of flops that neighbors the given list is a valid output*. You can use [this python script](https://tio.run/##VVBNa8QgEL3nV0xvSm1gN6UHYa/9Bb2JlDQZu4JMRF26@@uzakLSDCK8@Xpvnn@k60TdPA@ujxEI/76djYmVj8sGcoxowLjJs4jOCKA1XWKY/AMuUApK6i1dcDs47APjxyTeE9LIypySpJWUbycNr3WRIql50xQ6Qvt7/ZkCjsytdBGRMpVaWMwUwIIlcLsYa5ZU7expzOjlUpHKJHtfiYDpFgg@exdxK2AGx7Yy3PbeF812OWWd/Ao3XLQmLHaJalFcxbqsdLeSb5JN0Vcbdx7XVnNNPv3f/oMDsw@WEqtMqhNwFnAS8C6gq@@sBagFfWjO5yc "Python 3 – Try It Online") to verify if a given list of flops correctly neighbors a list.
You may take input and output in any reasonable format. For example, function arguments/return value, STDIN/STDOUT, reading/writing a file etc. are all valid. As usual, this is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so make the shortest program you can, and have fun! :)
  
[Answer]
# [Haskell](https://www.haskell.org/), ~~98~~ 71 bytes
```
h.r
h(x:s)|(a,b)<-span(/=x)s=l b:l s:h(b++r a)
h e=e
r=reverse
l=length
```
[Try it online!](https://tio.run/##VY3BqsIwEEX3@YpBXCS0KuqumK94y/dcpHaaFMc0zERx8b7dGEEENwP33HOZ4OSMRMXbvxLWrIK@d2L@tWt7c1hJclFv7N2IJeg7AumC7puGwRkVAC0qtow3ZEFFljD6HIqAhd9tu2v3bb1HpUaaEwjEyt@yzu6MFYiBpoGBa1@DurjpJQ2zgnTNP5lh8dpKB4tKeIoZluCr@KkZ5Up5ih5okvzljTMNBO/fuq5MeZxGcl7K6pTSEw "Haskell – Try It Online")
### Explanation
For a list of length  `n` this method produces `2*n` flops. It works by looking at the last element of the list, looking for the same element in the list before and flipping it to the second to last position. Then the list with the last element removed is recursively "neighboured".
For the list `[1,2,3,1,2]` the algorithm works like this:
```
[1,2,3,1,2]  flip longest prefix that ends in 2: flop 2
[2,1,3,1,2]  bring first element to second to last position: flop n-1 = flop 4
[1,3,1,2,2]  recursively work on n-1 list
[1,3,1,2]    there is no other 2: flop 0
[1,3,1,2]    flop n-1 = flop 3
[1,3,1,2]    recurse
[1,3,1]      flop 1
[1,3,1]      flop 2
[3,1,1]      recurse
[3,1]        flop 0
[3,1]        flop 1
 ...
```
All together this yields the flops `[2,4,0,3,1,2,0,1,0,0]` and the neighboured list `[3,1,1,2,2]`.
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 71 bytes
```
If[(n=Tr[1^#])<1,{},{i=Last@Ordering@#,n,n-1,i-1}~Join~#0[#~Drop~{i}]]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78H1CUmVcS/d8zLVojzzakKNowTjlW08ZQp7pWpzrT1iexuMTBvyglFagq3UFZJ08nT9dQJ1PXsLbOKz8zr07ZIFq5zqUov6CuOrM2Nlbtv4KDQrWxjoKRjoKhjoKJjoIxGBnVxv4HAA "Wolfram Language (Mathematica) – Try It Online")
## How it works
Given an array of length `n`, outputs a sequence of `4n` flops that sort the array in increasing order: in particular, putting duplicate elements next to each other.
The idea is that to sort an array, we move its largest element to the end, and then sort the first `n-1` elements of the array. To avoid implementing the flop operation, we move the largest element to the end in a way which does not disturb the other elements:
```
{3, 2, 1, 5, 3, 3, 2}    starting array, with largest element in position 4
{5, 1, 2, 3, 3, 3, 2}    flop 4 to put the largest element at the beginning
{2, 3, 3, 3, 2, 1, 5}    flop 7 to put the largest element at the end
{1, 2, 3, 3, 3, 2, 5}    flop 6 (7-1) to reverse the effect of flop 7 on other elements
{3, 2, 1, 3, 3, 2, 5}    flop 3 (4-1) to reverse the effect of flop 4 on other elements
```
In general, if the largest element is in position `i`, the sequence of flops that moves it to the end is `i, n, n-1, i-1`. 
[Answer]
# [Python 2](https://docs.python.org/2/), 69 bytes
```
l=input()
while l:k=l.index(max(l));print-~k,len(l),;l=l[:k:-1]+l[:k]
```
[Try it online!](https://tio.run/##PU09D4IwFNz5FS9MEIuhFYmBMOLq4kYYEF8CoRbSlIiLfx37ISR3fe29u@v0Ud0o2NqOTyyk7/srL3oxzSoIvXfXcwSeDQU/9uKJS/BqloCHYT7JXqjoOxCOQgsk5wWvsiGLaH0wl3rVTf/8Xc6YeQBKfswAwAVbMP/Zl63yjNripKC8XUspR@msD4nNsFaMQEogJnAiQAmcLWJ70tqrtMT02F3MuhK3tyG25VKrJ5shdRHXsYNtiIxLL13kYkFNaerITo4Jc6Tn@gc "Python 2 – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~19~~ 17 bytes
```
ỤỤạ‘Ḣ
ỤÇÐƤĖµUż’ṚF
```
Sorts the list.
[Try it online!](https://tio.run/##y0rNyan8///h7iUgtGvho4YZD3cs4gJyDrcfnnBsyZFph7aGHt3zqGHmw52z3P4fbn/UtObopIc7ZwDpyP//o6ONdBTMdBQMdBSMdRQMdRRMwcgATBrG6nApRAMFjcAMuEojsEoTiBqwRiOYXjOwuAlMgRlEC8wcODKCIV2QOrA0RJsFGBmCDDaDYCNjCDYxgmBD09hYAA "Jelly – Try It Online")
[Answer]
# [Clean](https://clean.cs.ru.nl), 88 bytes
I think there's a possibly shorter one with guards, but I haven't found it yet.
```
import StdEnv
$[h:t]#(a,b)=span((<>)h)t
=map length[b,t]++ $(b++r a)
$e=e
r=reverse
```
# 
```
$o r
```
[Try it online!](https://tio.run/##RU89b8MgFNz5FU@KB5CJkrZJhqpkaoZK2TK6HrDBMSofFpBI/fOlGPdDYrh73N2712vJbTJO3LQEw5VNykzOR7hEcbJ3VDXjc2xXmNOOsDBxi/HLkYwkImb4BFraaxybjsa2rqHCXV174ARVkknkmZd36YNMl8h9RCvQKkRg0DxSOFDYUsjggcKOwr6Ap9/Jvgh25S2Cw2Jpc8qg3ZRT8HsAC@sj4J81OPIPmUeBEMhthM@ymc2Wm@2zpXLgEVvY3AVtNjnnj1IYnBZ6WVC6/v8Rkr76QfNrSOu3c75InFVX4Oun5Ub14Rs "Clean – Try It Online")
As a function literal. Works the same way as [Laikoni's Haskell answer](https://codegolf.stackexchange.com/a/154453/18730), but golfed slightly differently, and of course also in a different language.
[Answer]
# JavaScript, 150 bytes
```
(a,f=n=>(a=[...a.slice(0, n).reverse(),...a.slice(n)],n),l=a.length,i=0)=>a.reduce(c=>[...c,f(a.indexOf(Math.max(...a.slice(0, l-i)))+1),f(l-i++)],[])
```
[Try it online!](https://tio.run/##VY7BasMwEETv@Qrdsos3S5LeAus/KP0A48Mirx0VRQqWGvL3rtJTcxkG5jEz3/rQ4tdwr4eUJ9uiVbfIBkqzJOlBZWBm5RKDNziSS8irPWwtBkj/ooQjJaQoytHSUq8U5IjSa8OnnwZ46V9VnmZQDmmy59cMn1qvfNMnvI/EQ0DE7oQNbr7rWvkw4t@7Sas6ccOJzvRBTcfdzudUcjSOeYH9HPO9XNzedW6BF424/QI "JavaScript (Node.js) – Try It Online")
# JavaScript, 151 bytes
```
a=>{f=n=>(a=[...a.slice(0,n).reverse(),...a.slice(n)],n),r=[];for(i=a.length+1;--i>0;)r.push(f(a.indexOf(Math.max(...a.slice(0, i)))+1),f(i));return r}
```
[Try it online!](https://tio.run/##VY5BasMwEEX3OYV2mcHyECe7GPkGpQcwXgz2yFZQJSMpIVBydsftqt18PrwH/9/4wXlMbi11iJNsXoqazcam@7YmmA7Y9ETElL0bBU46ICV5SMoCqP@QgMPOdDL90NqYwBkmL2EuS9W0de26U4uJ1ntewAKTC5M8Py18cFnoi5/wb0Q5RKwa1Bb21iYp9xRUev3em7iwMqpv9Flf9J7D4TDGkKMX8nGGo/VxzVd1VJWa4cdG3N4 "JavaScript (Node.js) – Try It Online")
  
Both basically sort the array by flipping the max number to the beginning and then flipping it to the back, repeating this with the remaining array. The first one uses reduce, the second one uses a for loop.
## Ungolfed:
```
array => {
  let flop = n => {
    array = [...array.slice(0, n).reverse(), ...array.slice(n)]; 
    return n;
  }
  let flops = [];
  for (let i = array.length + 1; --i > 0;) 
  {
    let maxIndex = array.indexOf(Math.max(...array.slice(0, i)));
    flops.push(flop(maxIndex + 1), flop(i));
  }
  return flops;
}
```
[Answer]
# Perl 5.10 (or higher), 66 bytes
Includes `+3` for `-n`
The `use 5.10.0` to bring the language to level perl 5.10 is considered free
```
#!/usr/bin/perl -n
use 5.10.0;
$'>=$&or$.=s/(\S+) \G(\S+)/$2 $1/*say"$. 2 $."while$.++,/\S+ /g
```
Run with the input as one line on STDIN:
```
flop.pl <<< "1 8 3 -5 6"
```
Sorts the list by repeatedly finding any inversion, flopping it to the front then flopping the inversion and flopping everything back to its old position.
It was surprisingly hard to get in the same ballpark as python on this one :-)
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~165~~ 160 bytes
* Thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat) for saving five bytes.
```
m,j,t;f(A,l)int*A;{for(j=0;j+j<l;A[j]=A[l+~j],A[l+~j++]=t)t=A[j];}F(A,l)int*A;{for(;l;f(A,++m),printf("%d,%d,",m,l),f(A,l--))for(j=m=0;j<l;j++)m=A[j]>A[m]?j:m;}
```
* [Try it online!](https://tio.run/##jY/daoNAEIWvm6dYhIJbR3A3ibTdbIr5e4aAeGE0KVnctKT2KthXtzO72D9K6c6IODvnO8cqfqyqvrdgoFWHMIOGH0/tTaYuh6dzaHSiTGRmjcpyU@gsb6I3U4B/R1GhW95qulLd5qdWNY4XRZbD8xkvDmFwXQN2ABZXwbnFMefeyZIXOiGXWwedZ7ktHsy9VV2/xeUSzGAwAPOAq4@gs9KJv5gFQByuhlHB4jkjDaUtcf7avuAUB93IlsdTyC@jK1xlWV7oi4QUEhiDgClWgo9gv5xOkaLUIlFbj/WMBTEESPaP4xk7zSQyFrAbGMvPHBJzTCgDJpIuU4rfEzdNacMzKi0pxxKqgbHyOXxJVzGK/shRa3aHjBXUA2NNDHK7xRIYIqWWY@qJpBbT74y9FvQva9gjo@vfAQ "C (gcc) – Try It Online")
* [Verification](https://tio.run/##fVDRboMwDHzvV2RvoLkVAUpbpL7uC/aG0MRKWJGigIBq69czn2GllaolJML2@e7i9jqcGxeN48kWfa@c@f6wdT94uPx0pXiVplKVbVqvN7Yi5eY01qlpr@qoUMjS/JZGvDlZU3Se/5g0P4NxpYe@LHV5lqZrnatXIcpcmvurFeScqb/On01nSs/Ocr0xjqWySaVqOlWr2im7mKmrKSXIwpUcvRwlylhkwWF1Zrh0Tr0Vtje3guHgEYbmTdG28FxPT5k737uLmbwOBuMiGVE/m7XsdBmlf7NcwZ8AFx27keFW/PQ7/ocJjG1Xu8ETpSykhAKKSNOWd8BHU65IcUEHFNKBM3uu7wjISOoxH3QwBGjfX90zIj8zPAdMkqEQbYUqFPmE41iyiSD@SAR7IM33Hr87sCak@UaTRo@GQ0ZskY5hXbN1TWgAPqFwth79a33aoew14GLiMI8BXNs7rvAfLrxlz5s9wiw3RfjiEB8nhRg@ZUyiGxCEdmiaTevnpsdf "Python 3 – Try It Online")
]