Ad
Algorithms

The method takes two arrays of integers a[n] and b[m]. Determine all integers that satisfy the following two conditions:

  • The elements of the first array are all factors of the integer being considered.
  • The integer being considered is a factor of all elements of the second array.

The method should return the numbers of integers that satisfy this condition.

def getTotalX(a: list, b: list) -> int:
    return sum(1 for elem in range(min(a)*int(max(a)/min(a)), min(b)+1, min(a)) if (all(elem % n==0 for n in a) and all(n % elem == 0 for n in b)))

Given a vector, calculate it's numerical gradient.

Examples:

gradient([3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3])
  = [-1, 1, 3, -2, -3, 1, 1, -1, -1, 1, 1]
gradient([1, 1, 2, -1, 7])
  = [0, 1, -3, 8]

Important: If the given vector has a size equal to n, the numerical derivative will be a vector with a size equal to n-1.

std::vector<int> gradient(const std::vector<int> &v) {
  std::vector<int> dv;
  
  for (int i = 1; i < int(v.size()); i++) 
    dv.push_back(v[i]-v[i-1]);
  
  return dv;
}
int convert(char bin) {
    int dec = 0;
  
    for (int i = 7; i >= 0; i--) {
      dec = dec << 1;
      if ((bin & (1 << i)) != 0)
        dec ++;
    }
    return dec;
}