Remove one redundant function and modify the structure to process the array from head to tail instead of after reversing the arrays.