Find prime factors of Array elements whose sum of exponents is divisible by K
Last Updated :
16 Aug, 2021
Given an array arr[] of N positive integers and an integer K., The task is to create a set of prime numbers such that the sum of all the powers of prime numbers in the prime factorization of all the array elements is divisible by K.
Examples:
Input: arr[] = {1, 2, 3}, K = 1
Output: {2, 3}
Explanation:
2 = 21
3 = 31
The power of 2 is 1 which is divisible by K(=1).
The power of 2 is 1 which is divisible by K(=1).
Input: arr[] = {2, 2, 4, 8}, K = 10
Output: {}
Explanation:
2 = 21
2 = 21
4 = 22
8 = 23
The power of 2 is (1 + 1 + 2 + 3) = 7 which is not divisible by K(=10).
Thus, the output empty set.
Naive approach: The idea is to find all prime numbers less than or equal to the maximum element of the array arr[]. For each prime number count number of times, it divides the array element. If the value of count is divisible by K, then insert the prime number into the resultant set. At the end print elements of the set.
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach the idea is to precompute the count of all the prime factors of all the numbers. Below are the steps:
- Create the smallest prime factorization array spf[] up to the maximum number in the array. This step is used to precalculate the prime factors of a number.
- Traverse the given array arr[] and for each element find the sum of all the count of factors stored in spf[] array.
- For each sum of the power of a prime number in the above steps stored it frequency in a Map.
- Traverse the map if, for any number, frequency is divisible by K then store that number.
- Finally, print all the numbers stored in the above step.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int spf[10001];
void spf_array( int spf[])
{
spf[1] = 1;
for ( int i = 2; i < 1000; i++)
spf[i] = i;
for ( int i = 4; i < 1000; i += 2)
spf[i] = 2;
for ( int i = 3; i * i < 1000; i++) {
if (spf[i] == i) {
for ( int j = i * i;
j < 1000; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
void frequent_prime( int arr[], int N,
int K)
{
spf_array(spf);
unordered_map< int , int > Hmap;
vector< int > result;
int i = 0;
int c = 0;
for (i = 0; i < N; i++) {
int x = arr[i];
while (x != 1) {
Hmap[spf[x]]
= Hmap[spf[x]] + 1;
x = x / spf[x];
}
}
Hmap.erase(1);
for ( auto x : Hmap) {
int primeNum = x.first;
int frequency = x.second;
if (frequency % K == 0) {
result.push_back(primeNum);
}
}
if (result.size() > 0) {
for ( auto & it : result) {
cout << it << ' ' ;
}
}
else {
cout << "{}" ;
}
}
int main()
{
int arr[] = { 1, 4, 6 };
int K = 1;
int N = sizeof (arr) / sizeof (arr[0]);
frequent_prime(arr, N, K);
}
|
Java
import java.util.*;
class GFG{
static int [] spf = new int [ 10001 ];
static void spf_array( int spf[])
{
spf[ 1 ] = 1 ;
for ( int i = 2 ; i < 1000 ; i++)
spf[i] = i;
for ( int i = 4 ; i < 1000 ; i += 2 )
spf[i] = 2 ;
for ( int i = 3 ; i * i < 1000 ; i++)
{
if (spf[i] == i)
{
for ( int j = i * i;
j < 1000 ; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
static void frequent_prime( int arr[], int N,
int K)
{
spf_array(spf);
Map<Integer, Integer> Hmap = new TreeMap<>();
ArrayList<Integer> result = new ArrayList<>();
int i = 0 ;
int c = 0 ;
for (i = 0 ; i < N; i++)
{
int x = arr[i];
while (x != 1 )
{
Hmap.put(spf[x],
Hmap.getOrDefault(spf[x], 0 ) + 1 );
x = x / spf[x];
}
}
Hmap.remove( 1 );
for (Map.Entry<Integer,
Integer> x : Hmap.entrySet())
{
int primeNum = x.getKey();
int frequency = x.getValue();
if (frequency % K == 0 )
{
result.add(primeNum);
}
}
if (result.size() > 0 )
{
for (Integer it : result)
{
System.out.print(it + " " );
}
}
else
{
System.out.print( "{}" );
}
}
public static void main (String[] args)
{
int arr[] = { 1 , 4 , 6 };
int K = 1 ;
int N = arr.length;
frequent_prime(arr, N, K);
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int [] spf = new int [10001];
static void spf_array( int [] spf)
{
spf[1] = 1;
for ( int i = 2; i < 1000; i++)
spf[i] = i;
for ( int i = 4; i < 1000; i += 2)
spf[i] = 2;
for ( int i = 3; i * i < 1000; i++)
{
if (spf[i] == i)
{
for ( int j = i * i; j < 1000; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
static void frequent_prime( int [] arr, int N, int K)
{
spf_array(spf);
SortedDictionary< int ,
int > Hmap = new SortedDictionary< int ,
int >();
List< int > result = new List< int >();
int i = 0;
for (i = 0; i < N; i++)
{
int x = arr[i];
while (x != 1)
{
if (Hmap.ContainsKey(spf[x]))
Hmap[spf[x]] = spf[x] + 1;
else
Hmap.Add(spf[x], 1);
x = x / spf[x];
}
}
Hmap.Remove(1);
foreach (KeyValuePair< int , int > x in Hmap)
{
int primeNum = x.Key;
int frequency = x.Value;
if (frequency % K == 0)
{
result.Add(primeNum);
}
}
if (result.Count > 0)
{
foreach ( int it in result)
{
Console.Write(it + " " );
}
}
else
{
Console.Write( "{}" );
}
}
public static void Main(String[] args)
{
int [] arr = {1, 4, 6};
int K = 1;
int N = arr.Length;
frequent_prime(arr, N, K);
}
}
|
Python3
spf = [ 0 for i in range ( 10001 )]
def spf_array(spf):
spf[ 1 ] = 1
for i in range ( 2 , 1000 , 1 ):
spf[i] = i
for i in range ( 4 , 1000 , 2 ):
spf[i] = 2
i = 3
while ( i * i < 1000 ):
if (spf[i] = = i):
j = i * i
while (j < 1000 ):
if (spf[j] = = j):
spf[j] = i
j + = i
i + = 1
def frequent_prime(arr, N, K):
spf_array(spf)
Hmap = {}
result = []
i = 0
c = 0
for i in range (N):
x = arr[i]
while (x ! = 1 ):
Hmap[spf[x]] = Hmap.get(spf[x], 0 ) + 1
x = x / / spf[x]
if ( 1 in Hmap):
Hmap.pop( 1 )
for key, value in Hmap.items():
primeNum = key
frequency = value
if (frequency % K = = 0 ):
result.append(primeNum)
result = result[:: - 1 ]
if ( len (result) > 0 ):
for it in result:
print (it, end = " " )
else :
print ( "{}" )
if __name__ = = '__main__' :
arr = [ 1 , 4 , 6 ]
K = 1
N = len (arr)
frequent_prime(arr, N, K)
|
Javascript
<script>
var spf = Array(10001);
function spf_array(spf)
{
spf[1] = 1;
for ( var i = 2; i < 1000; i++)
spf[i] = i;
for ( var i = 4; i < 1000; i += 2)
spf[i] = 2;
for ( var i = 3; i * i < 1000; i++) {
if (spf[i] == i) {
for ( var j = i * i;
j < 1000; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
function frequent_prime(arr, N, K)
{
spf_array(spf);
var Hmap = new Map();
var result = [];
var i = 0;
var c = 0;
for (i = 0; i < N; i++) {
var x = arr[i];
while (x != 1) {
if (Hmap.has(spf[x]))
Hmap.set(spf[x], Hmap.get(spf[x])+1)
else
Hmap.set(spf[x], 1);
x = parseInt(x / spf[x]);
}
}
Hmap. delete (1);
Hmap.forEach((value, key) => {
var primeNum = key;
var frequency = value;
if (frequency % K == 0) {
result.push(primeNum);
}
});
if (result.length > 0) {
result.forEach(it => {
document.write(it+ " " );
});
}
else {
document.write( "{}" );
}
}
var arr = [1, 4, 6];
var K = 1;
var N = arr.length;
frequent_prime(arr, N, K);
</script>
|
Output:
3 2
Time Complexity: O(M*log(M)), where M is the maximum element of the array.
Auxiliary Space: O(M)
Share your thoughts in the comments
Please Login to comment...