Senior
/ ** * Find two numbers in an array whose sum equals a given number * <p> * Write a function that takes an array of positive * unordered numbers as the first argument * and a positive number as the second argument. * The function should return true if there are 2 numbers in the array * that sum up to the second argument. * <p> * Input: ([10, 15, 3, 7], 17) * Output: true */
sobes.tech AI
Answer from AI
The hasTwoSum function uses a set to store "complementary" numbers needed to sum up to the target. As it iterates through the array, if the current number is in the set, it means two numbers have been found whose sum equals the target. This solution operates in O(n) time and O(n) space.