| The N given integers are distinct (I should've said so in .0). They
aren't being *sampled*, they're just *input* to the requested algorithm.
The distribution is given because the desired algorithm might only run
in 'expected' time O(N^1.5); this is still useful.
In p[a] + p[b] = p[c], the p[a] and p[b] needn't be distinct. But this offers
fewer than N additional triples -- small beans since the goal is O(N^1.5).
Similarly for p[a] and p[c] being equal.
I'm looking for something like the following O(N�) algorithm, but faster:
for a := 1 to N do
for b := 1 to N do
for c := 1 to N do
if p[a] + p[b] = p[c] then
writeln(p[a], p[b], p[c]);
The following is an O(N�) algorithm:
sort p[1..N] into ascending order
for c := 1 to N do
a := 1; { the p[a] increase }
b := N; { and the p[b] decrease }
while a <= N and b >= 1 do
if p[a] + p[b] = p[c] then
writeln(p[a], p[b], p[c]);
a := a + 1;
b := b - 1;
else if p[a] + p[b] < p[c]
then
a := a + 1;
else
b := b - 1;
|