Random commentary about Machine Learning, BigData, Spark, Deep Learning, C++, STL, Boost, Perl, Python, Algorithms, Problem Solving and Web Search
I could think of O(n^2logn) solutionFirst sort the array - O(nlogn).Then take three pointers - i,j,kfor i from 1 to n-2: j = i+1 k = n while j < k: if a[i]+a[j]+a[k] == 0: return (i,j,k) if a[i]+a[j]+a[k] > 0: k-- else: j++
I could think of O(n^2logn) solution
ReplyDeleteFirst sort the array - O(nlogn).
Then take three pointers - i,j,k
for i from 1 to n-2:
j = i+1
k = n
while j < k:
if a[i]+a[j]+a[k] == 0:
return (i,j,k)
if a[i]+a[j]+a[k] > 0:
k--
else:
j++