Created
August 19, 2013 11:17
-
-
Save loosechainsaw/6268045 to your computer and use it in GitHub Desktop.
Quadratic slow 3 Sum Problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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