Given an integer array a, size n, and an input integer value x.. Need to find all pairs of members a[i], a[j] such that (a[i],a[j],x) form a Pythagoras triplet, with x>a[i], a[j].
We need a double loop, the outer loop for a[i], and the inner loop finds a[j].
Here's a pseudocode.
int i, j, x, i2, x2, count=0;
input x;
x2=x*x; // economizing on cycles
if x<0 {x=-x};
for i=1 to n-1 {
if x>=i { // skip processing the impossible
i2=i*i; // economizing on cycles
for j=2 to n {
if x>=j { // skip processing the impossible
if i2+j*j==x2 {
count++;
print(i,j);
}
}
}
}
}
if count==0 { print("there are no matched pairs");
It will have a similar complexity as a bubble sort, n^2/2.