#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int threeSumClosest(vector<int> &num, int target) {
int closest = MIN_INT;
int diff = MAX_INT;
sort(num.begin(), num.end());
size_t size = num.size();
for (int i = 0; i < size; ++i)
{
int j = i+1;
int k = size-1;
while(j < k)
{
int sum = num[i] + num[j] + num[k];
int distance = abs(sum-target);
if (distance < diff)
{
closest = sum;
diff = distance;
}
if (sum < target)
{
++j
}
else
{
--k;
}
if (diff == 0) return closest;
}
}
return results;
}
ostream & operator <<(ostream & out, vector<vector<int> > &vv)
{
for (size_t i = 0; i < vv.size(); ++i)
{
vector<int> &v = vv[i];
for (size_t j = 0; j < v.size(); ++j)
{
out << v[j] << ", ";
}
out << endl;
}
return out;
}
int main(int argc, char const *argv[])
{
int a[] = {-1,0,1,2,-1,-4};
int sa = sizeof(a)/sizeof(int);
vector<int> va(a, a+sa);
vector<vector<int> > result = threeSum(va);
cout << result;
return 0;
}