I recently saw this simple 'two number sum' code in some website. While abalysing the time complexity, it was mentioned that the time complexity of the following code in O(N). I'm wondering if it is O(Nlog(N)) as the 'for' loop involves O(N) and the C++ STL method "find()" is used inside this loop, which may have a time complexity of O(log(N)). So I'm wondering if the overall complexity should be O(Nlog(N)) instead of O(N).
Apologies if this topic is already discussed before, but I couldn't find when I searched. I appreciate a good explanation if the time complexity is indeed O(N).
#include <bits/stdc++.h>
using namespace std;
void printPairs(int arr[], int arr_size, int sum)
{
unordered_set<int> s;
for (int i = 0; i < arr_size; i++)
{
int temp = sum - arr[i];
if (s.find(temp) != s.end())
cout << "Pair with given sum "
<< sum << " is (" << arr[i] << ","
<< temp << ")" << endl;
s.insert(arr[i]);
}
}