# Sum of Maximum and Minimum prime factor of every number in the Array

Given an array arr[], the task is to find the sum of the maximum and the minimum prime factor of every number in the given array.

Examples:

Input: arr[] = {15}
Output: 8
The maximum and the minimum prime factors
of 15 are 5 and 3 respectively.

Input: arr[] = {5, 10, 15, 20, 25, 30}
Output: 10 7 8 7 10 7

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use Sieve of Eratosthenes to precompute all the minimum and maximum prime factors of every number and store it in two arrays. After this precomputation, the sum of the minimum and the maximum prime factor can be found in constant time.

Below is the implementation of the above approach:

## CPP

 `// C++ implementation of the approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `const` `int` `MAX = 100000; ` ` `  `// max_prime[i] represent maximum prime ` `// number that divides the number i ` `int` `max_prime[MAX]; ` ` `  `// min_prime[i] represent minimum prime ` `// number that divides the number i ` `int` `min_prime[MAX]; ` ` `  `// Function to store the minimum prime factor ` `// and the maximum prime factor in two arrays ` `void` `sieve(``int` `n) ` `{ ` `    ``for` `(``int` `i = 2; i <= n; ++i) { ` ` `  `        ``// Check for prime number ` `        ``// if min_prime[i]>0, ` `        ``// then it is not a prime number ` `        ``if` `(min_prime[i] > 0) { ` `            ``continue``; ` `        ``} ` ` `  `        ``// if i is a prime number ` `        ``// min_prime number that divide prime number ` `        ``// and max_prime number that divide prime number ` `        ``// is the number itself. ` `        ``min_prime[i] = i; ` `        ``max_prime[i] = i; ` ` `  `        ``int` `j = i + i; ` ` `  `        ``while` `(j <= n) { ` `            ``if` `(min_prime[j] == 0) { ` ` `  `                ``// If this number is being visited ` `                ``// for first time then this divisor ` `                ``// must be the smallest prime number ` `                ``// that divides this number ` `                ``min_prime[j] = i; ` `            ``} ` ` `  `            ``// Update prime number till ` `            ``// last prime number that divides this number ` ` `  `            ``// The last prime number that ` `            ``// divides this number will be maximum. ` `            ``max_prime[j] = i; ` `            ``j += i; ` `        ``} ` `    ``} ` `} ` ` `  `// Function to find the sum of the minimum ` `// and the maximum prime factors of every ` `// number from the given array ` `void` `findSum(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``// Pre-calculation ` `    ``sieve(MAX); ` ` `  `    ``// For every element of the given array ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// The sum of its smallest ` `        ``// and largest prime factor ` `        ``int` `sum = min_prime[arr[i]] ` `                  ``+ max_prime[arr[i]]; ` ` `  `        ``cout << sum << ``" "``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 5, 10, 15, 20, 25, 30 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(``int``); ` ` `  `    ``findSum(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `class` `GFG ` `{ ` `     `  `    ``static` `int` `MAX = ``100000``;  ` `     `  `    ``// max_prime[i] represent maximum prime  ` `    ``// number that divides the number i  ` `    ``static` `int` `max_prime[] = ``new` `int``[MAX + ``1``];  ` `     `  `    ``// min_prime[i] represent minimum prime  ` `    ``// number that divides the number i  ` `    ``static` `int` `min_prime[] = ``new` `int``[MAX + ``1``];  ` `     `  `    ``// Function to store the minimum prime factor  ` `    ``// and the maximum prime factor in two arrays  ` `    ``static` `void` `sieve(``int` `n)  ` `    ``{  ` `        ``for` `(``int` `i = ``2``; i <= n; ++i) ` `        ``{  ` `     `  `            ``// Check for prime number  ` `            ``// if min_prime[i] > 0,  ` `            ``// then it is not a prime number  ` `            ``if` `(min_prime[i] > ``0``) ` `            ``{  ` `                ``continue``;  ` `            ``}  ` `     `  `            ``// if i is a prime number  ` `            ``// min_prime number that divide prime number  ` `            ``// and max_prime number that divide prime number  ` `            ``// is the number itself.  ` `            ``min_prime[i] = i;  ` `            ``max_prime[i] = i;  ` `     `  `            ``int` `j = i + i;  ` `     `  `            ``while` `(j <= n) ` `            ``{  ` `                ``if` `(min_prime[j] == ``0``) ` `                ``{  ` `     `  `                    ``// If this number is being visited  ` `                    ``// for first time then this divisor  ` `                    ``// must be the smallest prime number  ` `                    ``// that divides this number  ` `                    ``min_prime[j] = i;  ` `                ``}  ` `     `  `                ``// Update prime number till  ` `                ``// last prime number that divides this number  ` `     `  `                ``// The last prime number that  ` `                ``// divides this number will be maximum.  ` `                ``max_prime[j] = i;  ` `                ``j += i;  ` `            ``}  ` `        ``}  ` `    ``}  ` `     `  `    ``// Function to find the sum of the minimum  ` `    ``// and the maximum prime factors of every  ` `    ``// number from the given array  ` `    ``static` `void` `findSum(``int` `arr[], ``int` `n)  ` `    ``{  ` `     `  `        ``// Pre-calculation  ` `        ``sieve(MAX);  ` `     `  `        ``// For every element of the given array  ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{  ` `     `  `            ``// The sum of its smallest  ` `            ``// and largest prime factor  ` `            ``int` `sum = min_prime[arr[i]]  ` `                    ``+ max_prime[arr[i]];  ` `     `  `            ``System.out.print(sum + ``" "``);  ` `        ``}  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args) ` `    ``{  ` `        ``int` `arr[] = { ``5``, ``10``, ``15``, ``20``, ``25``, ``30` `};  ` `        ``int` `n = arr.length ; ` `     `  `        ``findSum(arr, n);  ` `     `  `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python

 `# Python3 implementation of the approach ` `MAX` `=` `100000` ` `  `# max_prime[i] represent maximum prime ` `# number that divides the number i ` `max_prime ``=` `[``0``]``*``(``MAX` `+` `1``) ` ` `  `# min_prime[i] represent minimum prime ` `# number that divides the number i ` `min_prime ``=` `[``0``]``*``(``MAX` `+` `1``) ` ` `  `# Function to store the minimum prime factor ` `# and the maximum prime factor in two arrays ` `def` `sieve(n): ` `    ``for` `i ``in` `range``(``2``, n ``+` `1``): ` ` `  `        ``# Check for prime number ` `        ``# if min_prime[i]>0, ` `        ``# then it is not a prime number ` `        ``if` `(min_prime[i] > ``0``): ` `            ``continue` ` `  `        ``# if i is a prime number ` `        ``# min_prime number that divide prime number ` `        ``# and max_prime number that divide prime number ` `        ``# is the number itself. ` `        ``min_prime[i] ``=` `i ` `        ``max_prime[i] ``=` `i ` ` `  `        ``j ``=` `i ``+` `i ` ` `  `        ``while` `(j <``=` `n): ` `            ``if` `(min_prime[j] ``=``=` `0``): ` ` `  `                ``# If this number is being visited ` `                ``# for first time then this divisor ` `                ``# must be the smallest prime number ` `                ``# that divides this number ` `                ``min_prime[j] ``=` `i ` ` `  `            ``# Update prime number till ` `            ``# last prime number that divides this number ` ` `  `            ``# The last prime number that ` `            ``# divides this number will be maximum. ` `            ``max_prime[j] ``=` `i ` `            ``j ``+``=` `i ` ` `  `# Function to find the sum of the minimum ` `# and the maximum prime factors of every ` `# number from the given array ` `def` `findSum(arr, n): ` ` `  `    ``# Pre-calculation ` `    ``sieve(``MAX``) ` ` `  `    ``# For every element of the given array ` `    ``for` `i ``in` `range``(n): ` ` `  `        ``# The sum of its smallest ` `        ``# and largest prime factor ` `        ``sum` `=` `min_prime[arr[i]] ``+` `max_prime[arr[i]] ` ` `  `        ``print``(``sum``, end ``=` `" "``) ` ` `  `# Driver code ` `arr ``=` `[``5``, ``10``, ``15``, ``20``, ``25``, ``30``] ` `n ``=` `len``(arr) ` ` `  `findSum(arr, n) ` ` `  `# This code is contributed by mohit kumar 29 `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG ` `{ ` `      `  `    ``static` `int` `MAX = 100000;  ` `      `  `    ``// max_prime[i] represent maximum prime  ` `    ``// number that divides the number i  ` `    ``static` `int` `[]max_prime = ``new` `int``[MAX + 1];  ` `      `  `    ``// min_prime[i] represent minimum prime  ` `    ``// number that divides the number i  ` `    ``static` `int` `[]min_prime = ``new` `int``[MAX + 1];  ` `      `  `    ``// Function to store the minimum prime factor  ` `    ``// and the maximum prime factor in two arrays  ` `    ``static` `void` `sieve(``int` `n)  ` `    ``{  ` `        ``for` `(``int` `i = 2; i <= n; ++i) ` `        ``{  ` `      `  `            ``// Check for prime number  ` `            ``// if min_prime[i] > 0,  ` `            ``// then it is not a prime number  ` `            ``if` `(min_prime[i] > 0) ` `            ``{  ` `                ``continue``;  ` `            ``}  ` `      `  `            ``// if i is a prime number  ` `            ``// min_prime number that divide prime number  ` `            ``// and max_prime number that divide prime number  ` `            ``// is the number itself.  ` `            ``min_prime[i] = i;  ` `            ``max_prime[i] = i;  ` `      `  `            ``int` `j = i + i;  ` `      `  `            ``while` `(j <= n) ` `            ``{  ` `                ``if` `(min_prime[j] == 0) ` `                ``{  ` `      `  `                    ``// If this number is being visited  ` `                    ``// for first time then this divisor  ` `                    ``// must be the smallest prime number  ` `                    ``// that divides this number  ` `                    ``min_prime[j] = i;  ` `                ``}  ` `      `  `                ``// Update prime number till  ` `                ``// last prime number that divides this number  ` `      `  `                ``// The last prime number that  ` `                ``// divides this number will be maximum.  ` `                ``max_prime[j] = i;  ` `                ``j += i;  ` `            ``}  ` `        ``}  ` `    ``}  ` `      `  `    ``// Function to find the sum of the minimum  ` `    ``// and the maximum prime factors of every  ` `    ``// number from the given array  ` `    ``static` `void` `findSum(``int` `[]arr, ``int` `n)  ` `    ``{  ` `      `  `        ``// Pre-calculation  ` `        ``sieve(MAX);  ` `      `  `        ``// For every element of the given array  ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{  ` `      `  `            ``// The sum of its smallest  ` `            ``// and largest prime factor  ` `            ``int` `sum = min_prime[arr[i]]  ` `                    ``+ max_prime[arr[i]];  ` `      `  `            ``Console.Write(sum + ``" "``);  ` `        ``}  ` `    ``}  ` `      `  `    ``// Driver code  ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{  ` `        ``int` `[]arr = { 5, 10, 15, 20, 25, 30 };  ` `        ``int` `n = arr.Length ; ` `      `  `        ``findSum(arr, n);  ` `      `  `    ``}  ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```10 7 8 7 10 7
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.