Algorithm that adds three numbers in an array that performs in O(n^2) time

21 Views Asked by At

Note that this question extends on this previous question.

Given an array A, and a value called value. Does there exist three elements in A where their sum equals value?

Note that repeating elements are allowed. For example: I have an array 1 2 4 5 and value, 3.

A[0] + A[0] + A[0] = 3


I created the naive brute-force O(n^3) algorithmic approach by myself, and with help through another question, I was able to break it to O(n^2log*n) time...

But there appears to be a way to break this even further to Θ(n^2) time. To be honest, I really don't know how that is possible (for the record, I am a beginner at this stuff). Is there an algorithm such as BinarySearch that could be useful to us?

I'd really appreciate any tips, pointers, or advice. Thanks in advance.