func two_sum(numbers, sum) { var (i, j) = (0, numbers.end) while (i < j) { given (sum <=> numbers[i]+numbers[j]) { when (-1) { --j } when (+1) { ++i } default { return [i, j] } } } return [] } say two_sum([0, 2, 11, 19, 90], 21) say two_sum([0, 2, 11, 19, 90], 25)
[1, 3] []
Last updated 5 years ago
Was this helpful?