You are given an array of integers "array []" and an input "x". you have to find if there are any two numbers in your array such that the sum of their square equals to x2 . in other words, you have to find out if there is any pair of i,j such that array[i]2 + array[j]2 = x 2 . if there are such pairs, you should print all such (i,j). otherwise print "there are no such pairs". example: array []: 6, -4, 6, 3, 9, 0, -1, -9, 2, 6 x: 5 you have 1 pair: (-4) 2 + (3) 2 = 5 2 so, your output would be corresponding array indices: (1, 3) note: the array size is 10 and you have to get the elements of the array from

Respuesta :

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.


ACCESS MORE
EDU ACCESS