use the smaller array for outer loop