loop on 1D array instead of 2D i,j