Problem 18 of array with custom solution