HackerRank Between Two Sets problem solution YASH PAL, 31 July 20249 August 2024 In this, Between Two Sets problem, There will be two arrays of integers. 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 These numbers are referred to as being between the two arrays. Determine how many such numbers exist. Hackerrank between two sets solution Problem solution in Python programming. input(); a=list(map(int, input().split())) b=list(map(int, input().split())) ans=0 for i in range(1, 101): if all(i%x==0 for x in a) and all(x%i==0 for x in b): ans+=1 print(ans) Problem solution in Java Programming. import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[] A = new int[n]; for (int i = 0; i < n; i++) { A[i] = in.nextInt(); } int[] B = new int[m]; for (int i = 0; i < m; i++) { B[i] = in.nextInt(); } int cnt = 0; for (int i = 1; i <= 100; i++) { boolean ok = true; for (int j = 0; j < n && ok; j++) { if (i % A[j] != 0) ok = false; } for (int j = 0; j < m && ok; j++) { if (B[j] % i != 0) ok = false; } if (ok) cnt++; } System.out.println(cnt); } } Problem solution in C++ programming. #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; int main(){ int n; int m; cin >> n >> m; vector<int> a(n); for(int a_i = 0;a_i < n;a_i++){ cin >> a[a_i]; } vector<int> b(m); for(int b_i = 0;b_i < m;b_i++){ cin >> b[b_i]; } int ans=0; for(int x=1;x<=100;x++){ bool ok=true; for(int i=0;i<n;i++) if(x%a[i]!=0) ok=false; for(int i=0;i<m;i++) if(b[i]%x!=0) ok=false; if(ok) ans++; } cout<<ans<<endl; return 0; } Problem solution in C programming. #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ int n, m; scanf("%d %d", &n, &m); int *a = malloc(sizeof(int) * n); for(int a_i = 0; a_i < n; a_i++){ scanf("%d",&a[a_i]); } int *b = malloc(sizeof(int) * m); for(int b_i = 0; b_i < m; b_i++){ scanf("%d",&b[b_i]); } int count = 0; for(int x=1; x<=100; x++){ int flag = 1; for(int a_i = 0; a_i < n; a_i++){ for(int b_i = 0; b_i < m; b_i++){ if(x % a[a_i] || b[b_i] % x) flag = 0; } } if(flag) count++; } printf("%d", count); return 0; } Problem solution in JavaScript programming. process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// isFactor = (a,b) => a % b === 0 function main() { var n_temp = readLine().split(' '); var n = parseInt(n_temp[0]); var m = parseInt(n_temp[1]); a = readLine().split(' '); a = a.map(Number); b = readLine().split(' '); b = b.map(Number); var results = [] for (var i = a[n-1];i <= b[0]; i++) { if (a.every((x)=> isFactor(i,x)) && b.every((x)=> isFactor(x,i))) results.push(i) } console.log(results.length) } algorithm coding problems AlgorithmsHackerRank
It is not necesarry to run code from 0 to 100 beause the between integer could not be greater than minimum of second array. So instead running loop from 1 to minimum of second array would work. for (int x = 1; x <= min; x++) { bool ok=true; for(int i=0;i<n;i++) { if(x%a[i]!=0) { ok=false; } } for(int i=0;i<m;i++) { if(b[i]%x!=0) { ok=false; } } if(ok) count++; }
PHP codes: $ans = 0; function is_after($x, $a){ foreach($a as $aa){ if($x % $aa != 0){ return False; } } return True; } function is_before($x, $b){ foreach($b as $bb){ if($bb % $x != 0){ return False; } } return True; } for($i=1; $i < 101; $i++){ if(is_after($i, $a) && is_before($i, $b)){ $ans++; } } return $ans;
C# codevar count = 0; for (int i = 1; i <= b.Min(); i++) { if (a.Where(x => i % x == 0).Count() == a.Count) { if (b.Where(c => c % i == 0).Count() == b.Count) { count++; } } } return count;
#python code ans = 0 for i in range(1, 101): if all(i % x == 0 for x in a) and all(x % i == 0 for x in b): ans += 1 return ans