Explain how you would solve the problem of finding two numbers in an array that sum to a target, and how you would use memoization to improve it.
Implement two_sum(nums, target), which returns the indices of two distinct elements whose values sum to target, or an empty array if no pair exists. Explain why caching previously seen values avoids repeated pair checks.
Assume the array contains at least two integers. Values and the target may be negative, and each element may be used only once.
def two_sum(nums, target):