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
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000
// Function to find the maximum sum of count
// of distinct prime factors of K array elements
int maxSumOfDistinctPrimeFactors(int arr[],
int N, int K)
{
// Stores the count of distinct primes
int CountDistinct[MAX + 1];
// Stores 1 and 0 at prime and
// non-prime indices respectively
bool prime[MAX + 1];
// Initialize the count of factors to 0
for (int i = 0; i <= MAX; i++) {
CountDistinct[i] = 0;
prime[i] = true;
}
// Sieve of Eratosthenes
for (long long int i = 2; i <= MAX; i++) {
if (prime[i] == true) {
// Count of factors of a
// prime number is 1
CountDistinct[i] = 1;
for (long long int j = i * 2; j <= MAX;
j += i) {
// Increment CountDistinct
// of all multiples of i
CountDistinct[j]++;
// Mark its multiples non-prime
prime[j] = false;
}
}
}
// Stores the maximum sum of distinct
// prime factors of K array elements
int sum = 0;
// Stores the count of all distinct
// prime factors
int PrimeFactor[20] = { 0 };
// Traverse the array to find
// count of all array elements
for (int i = 0; i < N; i++) {
PrimeFactor[CountDistinct[arr[i]]]++;
}
// Maximum sum of K prime factors
// of array elements
for (int i = 19; i >= 1; i--) {
// Check for the largest prime factor
while (PrimeFactor[i] > 0) {
// Increment sum
sum += i;
// Decrement its count and K
PrimeFactor[i]--;
K--;
if (K == 0)
break;
}
if (K == 0)
break;
}
// Print the maximum sum
cout << sum;
}
// Driver code
int main()
{
// Given array
int arr[] = { 6, 9, 12 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Given value of K
int K = 2;
maxSumOfDistinctPrimeFactors(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
public static int MAX = 1000000;
// Function to find the maximum sum of count
// of distinct prime factors of K array elements
static void maxSumOfDistinctPrimeFactors(int[] arr,
int N, int K)
{
// Stores the count of distinct primes
int[] CountDistinct = new int[MAX + 1];
// Stores 1 and 0 at prime and
// non-prime indices respectively
boolean[] prime = new boolean[MAX + 1];
// Initialize the count of factors to 0
for (int i = 0; i <= MAX; i++)
{
CountDistinct[i] = 0;
prime[i] = true;
}
// Sieve of Eratosthenes
for (int i = 2; i <= MAX; i++)
{
if (prime[i] == true)
{
// Count of factors of a
// prime number is 1
CountDistinct[i] = 1;
for (int j = i * 2; j <= MAX; j += i)
{
// Increment CountDistinct
// of all multiples of i
CountDistinct[j]++;
// Mark its multiples non-prime
prime[j] = false;
}
}
}
// Stores the maximum sum of distinct
// prime factors of K array elements
int sum = 0;
// Stores the count of all distinct
// prime factors
int[] PrimeFactor = new int[20];
// Traverse the array to find
// count of all array elements
for (int i = 0; i < N; i++)
{
PrimeFactor[CountDistinct[arr[i]]]++;
}
// Maximum sum of K prime factors
// of array elements
for (int i = 19; i >= 1; i--)
{
// Check for the largest prime factor
while (PrimeFactor[i] > 0)
{
// Increment sum
sum += i;
// Decrement its count and K
PrimeFactor[i]--;
K--;
if (K == 0)
break;
}
if (K == 0)
break;
}
// Print the maximum sum
System.out.print(sum);
}
// Driver code
public static void main(String[] args)
{
// Given array
int[] arr = { 6, 9, 12 };
// Size of the array
int N = arr.length;
// Given value of K
int K = 2;
maxSumOfDistinctPrimeFactors(arr, N, K);
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python 3 program for the above approach
MAX = 1000000
# Function to find the maximum sum of count
# of distinct prime factors of K array elements
def maxSumOfDistinctPrimeFactors(arr, N, K):
# Stores the count of distinct primes
CountDistinct = [0]*(MAX + 1)
# Stores 1 and 0 at prime and
# non-prime indices respectively
prime = [False]*(MAX + 1)
# Initialize the count of factors to 0
for i in range(MAX + 1):
CountDistinct[i] = 0
prime[i] = True
# Sieve of Eratosthenes
for i in range(2, MAX + 1):
if (prime[i] == True):
# Count of factors of a
# prime number is 1
CountDistinct[i] = 1
for j in range(i * 2, MAX + 1, i):
# Increment CountDistinct
# of all multiples of i
CountDistinct[j] += 1
# Mark its multiples non-prime
prime[j] = False
# Stores the maximum sum of distinct
# prime factors of K array elements
sum = 0
# Stores the count of all distinct
# prime factors
PrimeFactor = [0]*20
# Traverse the array to find
# count of all array elements
for i in range(N):
PrimeFactor[CountDistinct[arr[i]]] += 1
# Maximum sum of K prime factors
# of array elements
for i in range(19, 0, -1):
# Check for the largest prime factor
while (PrimeFactor[i] > 0):
# Increment sum
sum += i
# Decrement its count and K
PrimeFactor[i] -= 1
K -= 1
if (K == 0):
break
if (K == 0):
break
# Print the maximum sum
print(sum)
# Driver code
if __name__ == "__main__":
# Given array
arr = [6, 9, 12]
# Size of the array
N = len(arr)
# Given value of K
K = 2
maxSumOfDistinctPrimeFactors(arr, N, K)
# This code is contributed by chitranayal.
C#
using System;
public class GFG {
public static int MAX = 1000000;
// Function to find the maximum sum of count
// of distinct prime factors of K array elements
static void maxSumOfDistinctPrimeFactors(int[] arr,
int N, int K)
{
// Stores the count of distinct primes
int[] CountDistinct = new int[MAX + 1];
// Stores 1 and 0 at prime and
// non-prime indices respectively
bool [] prime = new bool[MAX + 1];
// Initialize the count of factors to 0
for (int i = 0; i <= MAX; i++) {
CountDistinct[i] = 0;
prime[i] = true;
}
// Sieve of Eratosthenes
for (int i = 2; i <= MAX; i++) {
if (prime[i] == true) {
// Count of factors of a
// prime number is 1
CountDistinct[i] = 1;
for (int j = i * 2; j <= MAX; j += i) {
// Increment CountDistinct
// of all multiples of i
CountDistinct[j]++;
// Mark its multiples non-prime
prime[j] = false;
}
}
}
// Stores the maximum sum of distinct
// prime factors of K array elements
int sum = 0;
// Stores the count of all distinct
// prime factors
int[] PrimeFactor = new int[20];
// Traverse the array to find
// count of all array elements
for (int i = 0; i < N; i++) {
PrimeFactor[CountDistinct[arr[i]]]++;
}
// Maximum sum of K prime factors
// of array elements
for (int i = 19; i >= 1; i--) {
// Check for the largest prime factor
while (PrimeFactor[i] > 0) {
// Increment sum
sum += i;
// Decrement its count and K
PrimeFactor[i]--;
K--;
if (K == 0)
break;
}
if (K == 0)
break;
}
// Print the maximum sum
Console.Write(sum);
}
// Driver code
static public void Main()
{
// Given array
int[] arr = { 6, 9, 12 };
// Size of the array
int N = arr.Length;
// Given value of K
int K = 2;
maxSumOfDistinctPrimeFactors(arr, N, K);
}
}
// This code is contributed by Dharanendra L V.
JavaScript
<script>
// JavaScript program of the above approach
let MAX = 1000000;
// Function to find the maximum sum of count
// of distinct prime factors of K array elements
function maxSumOfDistinctPrimeFactors( arr, N, K)
{
// Stores the count of distinct primes
let CountDistinct = [];
for (let i = 0; i <= MAX ; i++)
{
CountDistinct[i] = 0;
}
// Stores 1 and 0 at prime and
// non-prime indices respectively
let prime = [];
for (let i = 0; i <= MAX ; i++)
{
prime[i] = 0;
}
// Initialize the count of factors to 0
for (let i = 0; i <= MAX; i++)
{
CountDistinct[i] = 0;
prime[i] = true;
}
// Sieve of Eratosthenes
for (let i = 2; i <= MAX; i++)
{
if (prime[i] == true)
{
// Count of factors of a
// prime number is 1
CountDistinct[i] = 1;
for (let j = i * 2; j <= MAX; j += i)
{
// Increment CountDistinct
// of all multiples of i
CountDistinct[j]++;
// Mark its multiples non-prime
prime[j] = false;
}
}
}
// Stores the maximum sum of distinct
// prime factors of K array elements
let sum = 0;
// Stores the count of all distinct
// prime factors
let PrimeFactor = [];
for (let i = 0; i <= 20 ; i++)
{
PrimeFactor[i] = 0;
}
// Traverse the array to find
// count of all array elements
for (let i = 0; i < N; i++)
{
PrimeFactor[CountDistinct[arr[i]]]++;
}
// Maximum sum of K prime factors
// of array elements
for (let i = 19; i >= 1; i--)
{
// Check for the largest prime factor
while (PrimeFactor[i] > 0)
{
// Increment sum
sum += i;
// Decrement its count and K
PrimeFactor[i]--;
K--;
if (K == 0)
break;
}
if (K == 0)
break;
}
// Print the maximum sum
document.write(sum);
}
// Driver Code
// Given array
let arr = [ 6, 9, 12 ];
// Size of the array
let N = arr.length;
// Given value of K
let K = 2;
maxSumOfDistinctPrimeFactors(arr, N, K);
</script>
Time Complexity: O(N * log(log(N)))
Auxiliary Space: O(MAX)
Similar Reads
Count distinct prime factors for each element of an array Given an array arr[] of size N, the task is to find the count of distinct prime factors of each element of the given array. Examples: Input: arr[] = {6, 9, 12}Output: 2 1 2Explanation: 6 = 2 Ã 3. Therefore, count = 29 = 3 Ã 3. Therefore, count = 112 = 2 Ã 2 Ã 3. Therefore, count = 2The count of dist
15+ min read
Maximize count of distinct elements in a subsequence of size K in given array Given an array arr[] of N integers and an integer K, the task is to find the maximum count of distinct elements over all the subsequences of K integers. Example: Input: arr[]={1, 1, 2, 2}, K=3Output: 2Explanation: The subsequence {1, 1, 2} has 3 integers and the number of distinct integers in it are
4 min read
Maximize count of distinct elements possible in an Array from the given operation Given an array A[] of size N, the task is to maximize the count of distinct elements in the array by inserting the absolute differences of the existing array elements. Examples: Input: A[] = {1, 2, 3, 5} Output: 5 Explanation: Possible absolute differences among the array elements are: (2 - 1) = 1 (
6 min read
Maximum distinct prime factors of elements in a K-length subarray Given an array arr[] of N positive integers and an integer K, the task is to find the maximum distinct prime factors in a subarray of length K. Examples: Input: arr[] = {5, 9, 14, 6, 10, 77}, K=3Output: 5Explanation: The sub-array of length 3 with maximum distinct prime factors is 6, 10, 77 and prim
12 min read
Prime factors of LCM of array elements Given an array arr[] such that 1 <= arr[i] <= 10^12, the task is to find prime factors of LCM of array elements. Examples: Input : arr[] = {1, 2, 3, 4, 5, 6, 7, 8} Output : 2 3 5 7 // LCM of n elements is 840 and 840 = 2*2*2*3*5*7 // so prime factors would be 2, 3, 5, 7 Input : arr[] = {20, 10
15+ min read
Maximize the count of distinct elements in Array after at most K changes Given an array arr[], the task is to find the maximum number of distinct numbers in arr after at most K changes. In each change pick any element X from arr and change it to Y such that L <= Y <= R. Examples: Input: arr[] = {1, 2, 1, 4, 6, 4, 4}, L = 1, R = 5 and K = 2Output: 6Explanation: Foll
8 min read
Find prime factors of Array elements whose sum of exponents is divisible by K 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} Explanatio
12 min read
Count of distinct power of prime factor of N Given a positive integer N, the task is to find the total number of distinct power of prime factor of the given number N.Examples: Input: N = 216 Output: 4 Explanation: 216 can be expressed as 2 * 22 * 3 * 32. The factors satisfying the conditions are 2, 22, 3 and 32 as all of them are written as di
5 min read
Maximum sum of K-length subarray with maximum count of distinct prime factors Given an array arr[] consisting of N positive integers and an integer K, the task is to find the maximum sum of array elements in a subarray having maximum sum of distinct prime factors in each K-length subarray. Note: If there are multiple answers then print the sum of the original subarray having
11 min read
Maximum sum of K-length subarray consisting of same number of distinct elements as the given array Given an array arr[] consisting of N integers and an integer K, the task is to find a subarray of size K with maximum sum and count of distinct elements same as that of the original array. Examples: Input: arr[] = {7, 7, 2, 4, 2, 7, 4, 6, 6, 6}, K = 6Output: 31Explanation: The given array consists o
15+ min read