Reductor Array For two integer arrays, the comparator value is the total number of elements in the first array such that there exists no integer in the second array with an absolute difference less than or equal to d. Find the comparator value. For example there are two arrays a = [ 7,5, 9), b = 1 13, 1, 4), and the integer d = 3. The absolute difference of a[0] to b[0] = 17 - 13/= 6, b[1= 17-1 | = 6, and b[2] = 17-41= 3, to recap, the values are 6,6, 3. In this case, the absolute difference with b[2] is equal to d = 3, so this element does not meet the criterion. A similar analysis of a[1] = 5 yields absolute differences of 8, 4, 1 and of a[2] 9 yields 4, 8, 5. The only element of a that has an absolute difference with each element of b that is always greater than dis element a[2], thus the comparator value is 1. Function Description Complete the function comparatorValue in the editor below. The function must return an integer that denotes the comparator value of the arrays. comparatorValue has the following parameter(s): a[a[O),...a/n - 1]]: an array of integers b[b[0],...b[m - 1]]: an array of integers d: an integer

Respuesta :

Answer:

The function in Python is as follows:

def comparatorValue(a,b, d):

   count = 0; test = 0;

   for i in a:

       for j in b:

           if abs (i - j) <= d:

               test+=1

       if test == 0:

           count+=1

       test = 0

   print(count)

Explanation:

This defines the function

def comparatorValue(a,b, d):

The initializes the count and test to 0

   count = 0; test = 0;

This iterates through a

   for i in a:

This iterates through b

       for j in b:

This calculates absolute difference of elements in a and b. The absolute is then compared to d.

If the calculated difference is less or equal to d, the value is not a comparator value (test in then incremented)

           if abs (i - j) <= d:

               test+=1

The comparison ends here

If test is 0, then the value is a comparator value (count is incremented by 1)

      if test == 0:

           count+=1

Test is set to 0 for another iteration

      test = 0

This prints count

   print(count)