Count elements that are divisible by at-least one element in another array
Given two arrays arr1[] and arr2[]. The task is to find the count of such elements in the first array whose at-least one factor is present in the second array.
Examples:
Input : arr1[] = {10, 2, 13, 4, 15} ; arr2[] = {2, 4, 5, 6}
Output : 4
There is no factor of 13 which is present in the
second array. Except 13, factors of the rest 4
elements of the first array is present in the
second array.
Input : arr1[] = {11, 13, 17, 15} ; arr2[] = {3, 7, 9, 5}
Output : 1
The idea is to insert all elements of the second array into a hash such that the lookup for factors can be done in constant time. Now, traverse the first array and for every element generate all of the factors starting from 1 and check if any of the factors is present in the hash or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int elementCount( int arr1[], int n1, int arr2[], int n2)
{
int count = 0;
unordered_set< int > hash;
for ( int i = 0; i < n2; i++)
hash.insert(arr2[i]);
for ( int i = 0; i < n1; i++) {
for ( int j = 1; j * j <= arr1[i]; j++) {
if (arr1[i] % j == 0) {
if ((hash.find(j) != hash.end()) ||
(hash.find(arr1[i] / j) != hash.end())) {
count++;
break ;
}
}
}
}
return count;
}
int main()
{
int arr1[] = { 10, 2, 13, 4, 15 };
int arr2[] = { 2, 4, 5, 6 };
int n1 = sizeof (arr1) / sizeof (arr1[0]);
int n2 = sizeof (arr2) / sizeof (arr2[0]);
cout << elementCount(arr1, n1, arr2, n2);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int elementCount( int arr1[], int n1,
int arr2[], int n2)
{
int count = 0 ;
HashSet<Integer> hash = new HashSet<>();
for ( int i = 0 ; i < n2; i++)
{
hash.add(arr2[i]);
}
for ( int i = 0 ; i < n1; i++)
{
for ( int j = 1 ; j * j <= arr1[i]; j++)
{
if (arr1[i] % j == 0 )
{
if ((hash.contains(j) && j !=
( int ) hash.toArray()[hash.size() - 1 ]) ||
(hash.contains(arr1[i] / j) && (arr1[i] / j) !=
( int ) hash.toArray()[hash.size() - 1 ]))
{
count++;
break ;
}
}
}
}
return count;
}
public static void main(String[] args)
{
int arr1[] = { 10 , 2 , 13 , 4 , 15 };
int arr2[] = { 2 , 4 , 5 , 6 };
int n1 = arr1.length;
int n2 = arr2.length;
System.out.println(elementCount(arr1, n1, arr2, n2));
}
}
|
Python3
from math import sqrt
def elementCount(arr1, arr2):
count = 0
hash = frozenset (arr2)
for x in arr1:
for j in range ( 1 , int (sqrt(x)) + 1 ):
if x % j = = 0 :
if (j in hash or
x / j in hash ):
count + = 1
break
return count
arr1 = [ 10 , 2 , 13 , 4 , 15 ]
arr2 = [ 2 , 4 , 5 , 6 ]
print (elementCount(arr1, arr2))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
static int elementCount( int []arr1, int n1,
int []arr2, int n2)
{
int count = 0;
HashSet< int > hash = new HashSet< int >();
for ( int i = 0; i < n2; i++)
{
hash.Add(arr2[i]);
}
for ( int i = 0; i < n1; i++)
{
for ( int j = 1; j * j <= arr1[i]; j++)
{
if (arr1[i] % j == 0)
{
if ((hash.Contains(j) && j !=
( int ) hash.ToArray()[hash.Count- 1]) ||
(hash.Contains(arr1[i] / j) && (arr1[i] / j) !=
( int ) hash.ToArray()[hash.Count - 1]))
{
count++;
break ;
}
}
}
}
return count;
}
public static void Main(String[] args)
{
int []arr1 = {10, 2, 13, 4, 15};
int []arr2 = {2, 4, 5, 6};
int n1 = arr1.Length;
int n2 = arr2.Length;
Console.WriteLine(elementCount(arr1, n1, arr2, n2));
}
}
|
PHP
<?php
function elementCount( $arr1 , $arr2 )
{
$count = 0;
$hash = array_unique ( $arr2 );
foreach ( $arr1 as & $x )
for ( $j = 1; $j < (int)(sqrt( $x )) + 1; $j ++)
if ( $x % $j == 0)
{
if (in_array( $j , $hash ) ||
in_array((int)( $x / $j ), $hash ))
{
$count ++;
break ;
}
}
return $count ;
}
$arr1 = array ( 10, 2, 13, 4, 15 );
$arr2 = array ( 2, 4, 5, 6 );
print (elementCount( $arr1 , $arr2 ));
?>
|
Javascript
<script>
function elementCount(arr1 , n1 , arr2 , n2) {
var count = 0;
var hash = new Set();
for (i = 0; i < n2; i++) {
hash.add(arr2[i]);
}
for (i = 0; i < n1; i++) {
for (j = 1; j * j <= arr1[i]; j++) {
if (arr1[i] % j == 0) {
if ((hash.has(j) && j != parseInt( hash[hash.length - 1])
|| (hash.has(arr1[i] / j) && (arr1[i] / j) != parseInt( hash[hash.length - 1])))) {
count++;
break ;
}
}
}
}
return count;
}
var arr1 = [ 10, 2, 13, 4, 15 ];
var arr2 = [ 2, 4, 5, 6 ];
var n1 = arr1.length;
var n2 = arr2.length;
document.write(elementCount(arr1, n1, arr2, n2));
</script>
|
Output:
4
Last Updated :
28 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...