10 Two Sum
Naive Approach
Using `map`
Last updated
Last updated
bool hasArrayTwoCandidates(int arr[], int n, int x) {
for(int i = 0; i < n; i++)
for(int j = i + 1; j < n; j++)
if(arr[i] + arr[j] == x)
return true;
return false;
}bool hasArrayTwoCandidates(int arr[], int n, int x) {
unordered_set<int> visited;
for(int i = 0; i < n; i++)
if(visited.count(x - arr[i]))
return true;
else
visited.insert(arr[i]);
return false;
}