You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Find if there is a pair of numbers that sum to a given target value.
3
3
*
4
-
* Time Complexity:
5
-
* Space Complexity:
6
-
* Optimal Time Complexity:
4
+
* Time Complexity: O(n) where n is the length of the input array. This is because we iterate through the array once to check for pairs, and each lookup in the Set is O(1) on average. The overall time complexity is linear.
5
+
*
6
+
*
7
+
*
8
+
* Space Complexity: O(n) in the worst case, if all numbers in the input array are unique and we end up storing all of them in the Set. The space complexity is linear with respect to the size of the input array.
9
+
*
10
+
*
11
+
*
12
+
* Optimal Time Complexity: O(n) because we need to iterate through the array at least once to check for pairs. The use of a Set allows us to achieve this optimal time complexity by providing constant time lookups for the complement values.
7
13
*
8
14
* @param {Array<number>} numbers - Array of numbers to search through
9
15
* @param {number} target - Target sum to find
10
16
* @returns {boolean} True if pair exists, false otherwise
0 commit comments