a+b=-c . Therefore the solution is reducible to finding two numbers a,b such that a+b equals to -c.
1) sort the array : nlogn 2) Given a number k , you can find two numbers a,b such that a+b =k in O(n); 3) take a number x , repeat step 2 for k = -x such that a and b should not be x. 4) repeat step 3 for all the numbers in the array The complexity is n log n + n*n On Sun, Jul 22, 2012 at 9:44 AM, Victor Manuel Grijalva Altamirano < kavic1.mar...@gmail.com> wrote: > on solution it's with brute force, with three for's, > for i=0; i<lengthS (to find a) > for j=0; j<lengthS (to find b) > for k=0; k<lengthS (to find c) > if(i!=j and j!=k and i!=k) > if(s[i]+s[j]+s[k]==0)printf("find it") > > > other solution it's with binary search. > > 2012/7/22 lomash goyal <lomesh.go...@gmail.com> > >> Given an array *S* of *n* integers, are there elements *a*, *b*, *c* in * >> S* such that *a* + *b* + *c* = 0? Find all unique triplets in the array >> which gives the sum of zero. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To view this discussion on the web visit >> https://groups.google.com/d/msg/algogeeks/-/3cOK_JrSYEsJ. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > > > -- > Victor Manuel Grijalva Altamirano > Universidad Tecnologica de La Mixteca > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.