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.
9.2m questions
11.9m answers