Answer:
see image below
Step-by-step explanation:
This is a bit cumbersome, but it does the job. Not sure if it can be done much more efficiently. For these kind of operations, you'd be better off with a text based programming language.
5.9m questions
7.7m answers