Sum of Two Elements
Given an array of integers and a target integer, determine if there are two distinct indices i and j in the array such that array[i] + array[j] == target. Return true if such indices exist, otherwise return false.
[ [ 10, 15, 3, 7 ], 17 ]
Explanation. 10 and 7 sum to 17, which is the target.
[ [ 4, 5, 1, 2 ], 8 ]
Explanation. No two elements sum to 8.
[ [ 5, -2, 4, 9, 1 ], 7 ]
Explanation. -2 and 9 sum to 7, which is the target.
[ [], 5 ]
Explanation. The array is empty; no elements to sum.
[ [ 3, 3 ], 6 ]
Explanation. The element at index 0 and index 1 sum to target 6.
Follow-up: Can you implement a solution that is better than O(n^2) time complexity using additional data structure?
1. The number of elements in the input array will be at least 2.\n2. The elements of the array and the target are all integers.
- Views
- 2