Link to home
Start Free TrialLog in
Avatar of paul Noel
paul Noel

asked on

I need to find all pairs in an array equal to a sum complexity 0(n) right away and in c++

the array has series of numbers Need to find all pairs in the array using a hash table
integers ...
Avatar of sarabande
sarabande
Flag of Luxembourg image

the array has series of numbers

if you now have

std::vector< std::vector<int> > series; 

Open in new window


where some of the series elements are arrays with size 2, you could create a second array only containing pointers to pairs like

std::vector< std::vector<int, int> * > pairs;
std::vector< std::vector<int> >::iterator i;
for (i = series.begin(); i != series.end(); ++i)
    if (i->size() == 2) 
         pairs.push_back(&(*i));

Open in new window


but since you need to store the pointers it is not O(n).

Sara
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phoffric
phoffric

The question requires the following two items in identifying pairs of numbers that add up to a given SUM:
1) hash table
2) O(n) complexity.
The solution adheres to both of these requirements.