Skip to content

Instantly share code, notes, and snippets.

@loosechainsaw
Created August 19, 2013 11:17
Show Gist options
  • Save loosechainsaw/6268045 to your computer and use it in GitHub Desktop.
Save loosechainsaw/6268045 to your computer and use it in GitHub Desktop.
Quadratic slow 3 Sum Problem
#include <iostream>
template<class T>
T three_sum(T values[], int N){
T count = T();
for(int i = 0; i < N; ++i)
{
for(int j = i + 1; i < N; ++i)
{
for(int k = j + 1; i < N; ++i)
{
if(values[i] + values[j] + values[k] == 0) ++count;
}
}
}
return count;
}
int main(){
int values[] = {-10,-20,40,20,5,15,-30,60,-40,25,45};
using namespace std;
cout << "There are " << three_sum(values,11) << " that sum to zero\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment