Your question is Two Sum Efficiently. 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.
DICK'S Sporting Goods systems may need to identify two product prices whose combined value matches a target promotion amount. Given an array of integers nums and an integer target, return the indices of two distinct elements whose values add up to target.
Implement two_sum(nums, target), where nums is a list of integers and target is an integer. Return a list containing the two zero-based indices in ascending order of discovery. Do not use the same array element twice. Each valid input contains exactly one solution.
Your solution should improve on the brute-force O(n²) approach by using a data structure that supports efficient lookup of previously seen values.
def two_sum(nums, target):