Your question is Optimized Array Solution With Trade-offs. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Given an array of integers, write an optimized solution to find a specific target condition and explain your trade-offs against a brute-force approach.
For this task, the target condition is finding two distinct elements whose values sum to target. Return their zero-based indices, or [] if no pair exists. Explain why the optimized approach improves on checking every pair.
Function: def two_sum(nums, target):
Input: an integer array and integer target. Output: a list containing the two indices in ascending order, or an empty list.
def two_sum(nums, target):