Valentine Sum Pair

Hashmap Two Sum Array Hash Table Valentine

Problem: Valentine Sum Pair

On Valentine’s Day, love sums up perfectly - if you know where to look! In this problem, you are given an array of integers (which can be thought of as gift scores) and a target sum (for example, 14, in honor of February 14). Your task is to determine whether there exists a pair of distinct elements in the array whose sum equals the target.

If such a pair exists, return the pair as a two-element array (or list). If multiple valid pairs exist, returning any one is acceptable. If no valid pair exists, return an empty array (or list).

Example:

Input: arr = [2, 7, 11, 15], target = 9

Output: [2, 7]

Hint:

Try using a hash map (or dictionary) to keep track of the numbers you have seen so far to achieve an O(n) time solution.

Good luck and Happy Valentine’s Day!