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.
7.8m questions
10.5m answers