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
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 <bits/stdc++.h> 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 |
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.
Recommended Posts:
- Find sum of a number and its maximum prime factor
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Minimum and Maximum prime numbers in an array
- k-th prime factor of a given number
- N-th prime factor of a given number
- Find largest prime factor of a number
- Sum of largest prime factor of each number less than equal to n
- Insert minimum number in array so that sum of array becomes prime
- Find prime number K in an array such that (A[i] % K) is maximum
- Maximum and minimum of an array using minimum number of comparisons
- Minimum inverting factor in an array
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Count minimum factor jumps required to reach the end of an Array
- Find a number such that maximum in array is minimum possible after XOR
- Minimum and Maximum element of an array which is divisible by a given number k
- Number of times Maximum and minimum value updated during traversal of array
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Prime Factor
- Minimum and Maximum Prime Numbers of a Singly Linked List
- Exactly n distinct prime factor numbers from a to b
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.

