Thursday, August 4, 2011

Salesforce Question: In an unsorted array, find a pair (a, b) such that a+b = k

It was given that pair will be unique.
void FindPair(int* arr, int size, int sum)
{
    for(int i=0; i<size; ++i)
          hash[arr[i]] = 1;
    for(int i=0; i<size; ++i)
    {
           if(hash[sum-arr[i]])
                cout<<"Pair found ( " <<arr[i]<<", "<<sum-arr[i]<<" )";
      }
}


No comments:

Post a Comment