build next arrary
Implement to build the next array efficiently