#CJam, 14 bytes I know Martin has 13 byte solution, but here goes a 14 one: q~2bf*{\1m<^}* [Try it online here][1] [1]: http://cjam.aditsu.net/#code=q~2bf*%7B%5C1m%3C%5E%7D*&input=14%2013