I am trying to write a linear-time algorithm O(n), which given a table A[0..n-1] (filled with ascending natural values) checks if there is a pair A[i], A[j] which satisfies f(A[i], A[j]) = C (C is a predefined constant).
Supposing that f(a,b) = a+b the algorithm will be:
Algo Paires(A[0..N-1], C)
in: Tab A[0..n-1] and C a constant
out : Boolean
Init indice ← 0
For i ← 0..n-1 do
if indice ≠ i & A[indice] + A[i] = C
return true
else if i = n-1 & indice ≤ n-2
indice++; i ← 0;
End for
return False
But if:
what will be the algorithm then? any suggestions ?