RBC interview question

Array manipulation, improve code effiency