Between Two Sets Solution in CPP | HackerRank | Between Two Sets HackerRank CPP Solution | Between Two Sets Solution HackerRank | Between Two Sets Kangaroo Solution


Between Two Sets Solution in CPP | HackerRank | Between Two Sets HackerRank Solution | Between Two Sets Solution HackerRank | Between Two Sets Solution


Between Two Sets Solution in CPP

Between Two Sets Solution in CPP

Between Two Sets HackerRank Solution in CPP


Problem:

You will be given two arrays of integers and asked to determine all integers that satisfy the following two conditions:

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

These numbers are referred to as being between the two arrays. You must determine how many such numbers exist.

For example, given the arrays  and , there are two numbers between them:  and  and  for the first value. Similarly,  and .

Function Description

Complete the getTotalX function in the editor below. It should return the number of integers that are betwen the sets.

getTotalX has the following parameter(s):

  • a: an array of integers
  • b: an array of integers

Input Format

The first line contains two space-separated integers,  and , the number of elements in array  and the number of elements in array .
The second line contains  distinct space-separated integers describing  where .
The third line contains  distinct space-separated integers describing  where .

Constraints

Output Format

Print the number of integers that are considered to be between  and .

Sample Input

2 3
2 4
16 32 96

Sample Output

3

Explanation

2 and 4 divide evenly into 4, 8, 12 and 16.
4, 8 and 16 divide evenly into 16, 32, 96.

4, 8 and 16 are the only three numbers for which each element of a is a factor and each is a factor of all elements of b.


https://www.hackerrank.com/challenges/between-two-sets/problem

Between Two Sets HackerRank Solution in CPP

Between Two Sets Solution 

#include <iostream> #include <vector> #include <algorithm> unsigned int count_between_two_sets(std::vector<unsigned short>& factors, std::vector<unsigned short>& multiples) { std::sort(factors.begin(), factors.end()); std::sort(multiples.begin(), multiples.end()); unsigned int count = 0; unsigned short times_multiplied = 2; unsigned short num = factors.back(); while ( num <= multiples.front()) { bool is_multiple = true; bool is_factor = true; for (unsigned int i = 0; i < factors.size(); ++i) { if (num % factors[i] != 0) { is_multiple = false; break; } } for (unsigned int j = 0; j < multiples.size(); ++j) { if (is_multiple && multiples[j] % num != 0) { is_factor = false; break; } } if (is_multiple && is_factor) { count++; } num = factors.back() * times_multiplied; times_multiplied++; } return count; } int main() { unsigned short num_factors, num_multiples; std::cin >> num_factors >> num_multiples; std::vector<unsigned short> factors(num_factors); std::vector<unsigned short> multiples(num_multiples); for (unsigned short i = 0; i < num_factors; ++i) { std::cin >> factors[i]; } for (unsigned short i = 0; i < num_multiples; ++i) { std::cin >> multiples[i]; } std::cout << count_between_two_sets(factors, multiples) << "\n"; }

Post a Comment

0 Comments