Maximize sum of count of distinct prime factors of K array elements
Last Updated :
06 Jul, 2021
Given an array arr[] of size N, the task is to find the maximum sum possible of the count of distinct prime factors of K array elements.
Examples:
Input: arr[] = {6, 9, 12}, K = 2
Output: 4
Explanation:
Distinct prime factors of 6, 9, 12 are 2, 1, 2.
K elements whose distinct prime factors are maximum are 6 and 12. Therefore, sum of their count = 2 + 2 = 4.
Input: arr[] = {4, 8, 10, 6}, K = 3
Output: 5
Explanation:
Distinct prime factors of 4, 8, 10, 6 are 1, 1, 2, 2.
K elements whose distinct prime factors are maximum are 4, 6, 10. Therefore, sum of their count = 1 + 2 + 2 = 5.
Approach: Follow the steps below to solve the problem:
- Initialize a boolean array prime[] of size 106 to store whether the number is prime or not by Sieve of Eratosthenes technique.
- Initialize an array CountDistinct[] to store the number of distinct prime factors of numbers.
- Increment the count of prime factors in its multiples, while marking the number as prime.
- Maximum number of distinct prime numbers of a number less than 106 is 8 i.e, (2 × 3 × 5 × 7 × 11 × 13 × 17 × 19 = 9699690 > 106).
- Initialize a variable, say sum to store the maximum sum of distinct prime factors of K array elements.
- Initialize an array PrimeFactor[] of size 20 to store the count of all distinct prime factors and initialize it to 0.
- Now traverse the array arr[] and increment PrimeFactor[CountDistinct[arr[i]]]++.
- Traverse the array PrimeFactor[] from backward and increment sum up to K times till it becomes 0.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000
int maxSumOfDistinctPrimeFactors( int arr[],
int N, int K)
{
int CountDistinct[MAX + 1];
bool prime[MAX + 1];
for ( int i = 0; i <= MAX; i++) {
CountDistinct[i] = 0;
prime[i] = true ;
}
for ( long long int i = 2; i <= MAX; i++) {
if (prime[i] == true ) {
CountDistinct[i] = 1;
for ( long long int j = i * 2; j <= MAX;
j += i) {
CountDistinct[j]++;
prime[j] = false ;
}
}
}
int sum = 0;
int PrimeFactor[20] = { 0 };
for ( int i = 0; i < N; i++) {
PrimeFactor[CountDistinct[arr[i]]]++;
}
for ( int i = 19; i >= 1; i--) {
while (PrimeFactor[i] > 0) {
sum += i;
PrimeFactor[i]--;
K--;
if (K == 0)
break ;
}
if (K == 0)
break ;
}
cout << sum;
}
int main()
{
int arr[] = { 6, 9, 12 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 2;
maxSumOfDistinctPrimeFactors(arr, N, K);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int MAX = 1000000 ;
static void maxSumOfDistinctPrimeFactors( int [] arr,
int N, int K)
{
int [] CountDistinct = new int [MAX + 1 ];
boolean [] prime = new boolean [MAX + 1 ];
for ( int i = 0 ; i <= MAX; i++)
{
CountDistinct[i] = 0 ;
prime[i] = true ;
}
for ( int i = 2 ; i <= MAX; i++)
{
if (prime[i] == true )
{
CountDistinct[i] = 1 ;
for ( int j = i * 2 ; j <= MAX; j += i)
{
CountDistinct[j]++;
prime[j] = false ;
}
}
}
int sum = 0 ;
int [] PrimeFactor = new int [ 20 ];
for ( int i = 0 ; i < N; i++)
{
PrimeFactor[CountDistinct[arr[i]]]++;
}
for ( int i = 19 ; i >= 1 ; i--)
{
while (PrimeFactor[i] > 0 )
{
sum += i;
PrimeFactor[i]--;
K--;
if (K == 0 )
break ;
}
if (K == 0 )
break ;
}
System.out.print(sum);
}
public static void main(String[] args)
{
int [] arr = { 6 , 9 , 12 };
int N = arr.length;
int K = 2 ;
maxSumOfDistinctPrimeFactors(arr, N, K);
}
}
|
Python3
MAX = 1000000
def maxSumOfDistinctPrimeFactors(arr, N, K):
CountDistinct = [ 0 ] * ( MAX + 1 )
prime = [ False ] * ( MAX + 1 )
for i in range ( MAX + 1 ):
CountDistinct[i] = 0
prime[i] = True
for i in range ( 2 , MAX + 1 ):
if (prime[i] = = True ):
CountDistinct[i] = 1
for j in range (i * 2 , MAX + 1 , i):
CountDistinct[j] + = 1
prime[j] = False
sum = 0
PrimeFactor = [ 0 ] * 20
for i in range (N):
PrimeFactor[CountDistinct[arr[i]]] + = 1
for i in range ( 19 , 0 , - 1 ):
while (PrimeFactor[i] > 0 ):
sum + = i
PrimeFactor[i] - = 1
K - = 1
if (K = = 0 ):
break
if (K = = 0 ):
break
print ( sum )
if __name__ = = "__main__" :
arr = [ 6 , 9 , 12 ]
N = len (arr)
K = 2
maxSumOfDistinctPrimeFactors(arr, N, K)
|
C#
using System;
public class GFG {
public static int MAX = 1000000;
static void maxSumOfDistinctPrimeFactors( int [] arr,
int N, int K)
{
int [] CountDistinct = new int [MAX + 1];
bool [] prime = new bool [MAX + 1];
for ( int i = 0; i <= MAX; i++) {
CountDistinct[i] = 0;
prime[i] = true ;
}
for ( int i = 2; i <= MAX; i++) {
if (prime[i] == true ) {
CountDistinct[i] = 1;
for ( int j = i * 2; j <= MAX; j += i) {
CountDistinct[j]++;
prime[j] = false ;
}
}
}
int sum = 0;
int [] PrimeFactor = new int [20];
for ( int i = 0; i < N; i++) {
PrimeFactor[CountDistinct[arr[i]]]++;
}
for ( int i = 19; i >= 1; i--) {
while (PrimeFactor[i] > 0) {
sum += i;
PrimeFactor[i]--;
K--;
if (K == 0)
break ;
}
if (K == 0)
break ;
}
Console.Write(sum);
}
static public void Main()
{
int [] arr = { 6, 9, 12 };
int N = arr.Length;
int K = 2;
maxSumOfDistinctPrimeFactors(arr, N, K);
}
}
|
Javascript
<script>
let MAX = 1000000;
function maxSumOfDistinctPrimeFactors( arr, N, K)
{
let CountDistinct = [];
for (let i = 0; i <= MAX ; i++)
{
CountDistinct[i] = 0;
}
let prime = [];
for (let i = 0; i <= MAX ; i++)
{
prime[i] = 0;
}
for (let i = 0; i <= MAX; i++)
{
CountDistinct[i] = 0;
prime[i] = true ;
}
for (let i = 2; i <= MAX; i++)
{
if (prime[i] == true )
{
CountDistinct[i] = 1;
for (let j = i * 2; j <= MAX; j += i)
{
CountDistinct[j]++;
prime[j] = false ;
}
}
}
let sum = 0;
let PrimeFactor = [];
for (let i = 0; i <= 20 ; i++)
{
PrimeFactor[i] = 0;
}
for (let i = 0; i < N; i++)
{
PrimeFactor[CountDistinct[arr[i]]]++;
}
for (let i = 19; i >= 1; i--)
{
while (PrimeFactor[i] > 0)
{
sum += i;
PrimeFactor[i]--;
K--;
if (K == 0)
break ;
}
if (K == 0)
break ;
}
document.write(sum);
}
let arr = [ 6, 9, 12 ];
let N = arr.length;
let K = 2;
maxSumOfDistinctPrimeFactors(arr, N, K);
</script>
|
Time Complexity: O(N * log(log(N)))
Auxiliary Space: O(MAX)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...