Count elements that are divisible by at-least one element in another array
Last Updated :
28 Dec, 2021
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++
// CPP program to find count of
// elements in first array whose
// atleast one factor is present
// in second array.
#include <bits/stdc++.h>
using namespace std;
// Util function to count the elements
// in first array whose atleast
// one factor is present in second array
int elementCount(int arr1[], int n1, int arr2[], int n2)
{
// counter to count number of elements
int count = 0;
// Hash of second array elements
unordered_set<int> hash;
for (int i = 0; i < n2; i++)
hash.insert(arr2[i]);
// loop to traverse through array elements
// and check for its factors
for (int i = 0; i < n1; i++) {
// generate factors of elements
// of first array
for (int j = 1; j * j <= arr1[i]; j++) {
// Check if j is a factor
if (arr1[i] % j == 0) {
// check if the factor is present in
// second array using the hash
if ((hash.find(j) != hash.end()) ||
(hash.find(arr1[i] / j) != hash.end())) {
count++;
break;
}
}
}
}
return count;
}
// Driver code
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
// Java program to find count of
// elements in first array whose
// atleast one factor is present
// in second array.
import java.util.*;
class GFG
{
// Util function to count the elements
// in first array whose atleast
// one factor is present in second array
static int elementCount(int arr1[], int n1,
int arr2[], int n2)
{
// counter to count number of elements
int count = 0;
// Hash of second array element
HashSet<Integer> hash = new HashSet<>();
for (int i = 0; i < n2; i++)
{
hash.add(arr2[i]);
}
// loop to traverse through array elements
// and check for its factors
for (int i = 0; i < n1; i++)
{
// generate factors of elements
// of first array
for (int j = 1; j * j <= arr1[i]; j++)
{
// Check if j is a factor
if (arr1[i] % j == 0)
{
// check if the factor is present in
// second array using the hash
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;
}
// Driver code
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));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python program to find count of
# elements in first array whose
# atleast one factor is present
# in second array.
# Importing sqrt() function
from math import sqrt
# Util function to count the
# elements in first array
# whose atleast one factor is
# present in second array
def elementCount(arr1, arr2):
# counter to count
# number of elements
count = 0
# Hash of second array elements
hash = frozenset(arr2)
# loop to traverse through array
# elements and check for its factors
for x in arr1:
# generate factors of
# elements of first array
for j in range(1, int(sqrt(x)) + 1):
# Check if j is a factor
if x % j == 0:
# check if the factor is present
# in second array using the hash
if (j in hash or
x / j in hash):
count+=1
break
return count
# Driver code
arr1 = [ 10, 2, 13, 4, 15 ]
arr2 = [ 2, 4, 5, 6 ]
print(elementCount(arr1, arr2))
# This code is contributed
# by vaibhav29498
C#
// C# program to find count of
// elements in first array whose
// atleast one factor is present
// in second array.
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
// Util function to count the elements
// in first array whose atleast
// one factor is present in second array
static int elementCount(int []arr1, int n1,
int []arr2, int n2)
{
// counter to count number of elements
int count = 0;
// Hash of second array element
HashSet<int> hash = new HashSet<int>();
for (int i = 0; i < n2; i++)
{
hash.Add(arr2[i]);
}
// loop to traverse through array elements
// and check for its factors
for (int i = 0; i < n1; i++)
{
// generate factors of elements
// of first array
for (int j = 1; j * j <= arr1[i]; j++)
{
// Check if j is a factor
if (arr1[i] % j == 0)
{
// check if the factor is present in
// second array using the hash
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;
}
// Driver code
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));
}
}
// This code contributed by Rajput-Ji
PHP
<?php
// PHP program to find count of
// elements in first array whose
// atleast one factor is present
// in second array.
// Util function to count the
// elements in first array
// whose atleast one factor is
// present in second array
function elementCount($arr1, $arr2)
{
// counter to count
// number of elements
$count = 0;
// Hash of second array elements
$hash = array_unique($arr2);
// loop to traverse through array
// elements and check for its factors
foreach($arr1 as &$x)
// generate factors of
// elements of first array
for ($j = 1; $j < (int)(sqrt($x)) + 1; $j++)
// Check if j is a factor
if ($x % $j == 0)
{
// check if the factor is present
// in second array using the hash
if (in_array($j, $hash) ||
in_array((int)($x / $j), $hash))
{
$count++;
break;
}
}
return $count;
}
// Driver code
$arr1 = array( 10, 2, 13, 4, 15 );
$arr2 = array( 2, 4, 5, 6 );
print(elementCount($arr1, $arr2));
// This code is contributed mits
?>
JavaScript
<script>
// javascript program to find count of
// elements in first array whose
// atleast one factor is present
// in second array.
// Util function to count the elements
// in first array whose atleast
// one factor is present in second array
function elementCount(arr1 , n1 , arr2 , n2) {
// counter to count number of elements
var count = 0;
// Hash of second array element
var hash = new Set();
for (i = 0; i < n2; i++) {
hash.add(arr2[i]);
}
// loop to traverse through array elements
// and check for its factors
for (i = 0; i < n1; i++) {
// generate factors of elements
// of first array
for (j = 1; j * j <= arr1[i]; j++) {
// Check if j is a factor
if (arr1[i] % j == 0) {
// check if the factor is present in
// second array using the hash
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;
}
// Driver code
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));
// This code contributed by umadevi9616
</script>
Output:
4
Similar Reads
Find array elements that are divisible by at least one other element Given an array arr[] of positive integers, count all special numbers in the array. A number is considered a special number if it is divisible by at least one other number in the same array.Examples : Input : arr[] = [1, 2, 3] Output : 2Explanation : 2 and 3 are divisible by 1.Input : arr[] = [2, 3,
9 min read
Elements of an array that are not divisible by any element of another array Given two arrays A[] and B[], write an efficient code to determine if every element of B[] is divisible by at least 1 element of A[]. Display those elements of B[], which are not divisible by any of the elements in A[]. Examples : Input : A[] = {100, 200, 400, 100, 600} B[] = {45, 90, 48, 1000, 3000
10 min read
Count numbers in a range that are divisible by all array elements Given N numbers and two numbers L and R, the task is to print the count of numbers in the range [L, R] which are divisible by all the elements of the array. Examples: Input: a[] = {1, 4, 2], L = 1, R = 10 Output : 2 In range [1, 10], the numbers 4 and 8 are divisible by all the array elements. Input
8 min read
Count numbers from a given range that are not divisible by any of the array elements Given an array arr[] consisting of N positive integers and integers L and R, the task is to find the count of numbers in the range [L, R] which are not divisible by any of the array elements. Examples: Input: arr[] = {2, 3, 4, 5, 6}, L = 1, R = 20Output: 6Explanation:The 6 numbers in the range [1, 2
7 min read
Count of elements in given Array divisible by all elements in their prefix Given an array arr[] containing N positive integers, the task is to find the total number of elements in the array that are divisible by all the elements present before them. Examples: Input: arr[] = {10, 6, 60, 120, 30, 360}Output: 3Explanation: 60, 120 and 360 are the required elements. Input: arr
12 min read
Count the number of elements in an array which are divisible by k Given an array of integers. The task is to calculate the count of a number of elements which are divisible by a given number k. Examples: Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 3 Output: 3 Numbers which are divisible by k are { 6, 12, 18 } Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 2 Output: 5
6 min read