Pass the array one time to get n, then it would be easy