Given an array arr[], the task is to check whether the sum of primes in the array is divisible by any of the primes in the array. If it is then print YES, otherwise print NO.
Examples:
Input: arr[] = {2, 3}
Output: NO
Primes: 2, 3
Sum = 2 + 3 = 5 which is neither divisible by 2 nor 3Input: arr[] = {1, 2, 3, 4, 5}
Output: YES
2 + 3 + 5 = 10 is divisible by 2 as well as 5
Approach: The idea is to generate all primes upto maximum element from the array using Sieve of Eratosthenes.
- Traverse the array and check if the current element is prime or not. If it is prime then update sum = sum + arr[i].
- Traverse the array again and check if sum % arr[i] = 0 where arr[i] is prime. If it is then print YES. Otherwise print NO in the end.
Below is the implementation of the above approach:
C++
// C++ program to check if sum of primes from an array // is divisible by any of the primes from the same array #include <bits/stdc++.h> using namespace std; // Function to print "YES" if sum of primes from an array // is divisible by any of the primes from the same array void SumDivPrime(int A[], int n) { int max_val = *(std::max_element(A, A + n)) + 1; // USE SIEVE TO FIND ALL PRIME NUMBERS LESS // THAN OR EQUAL TO max_val // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. vector<bool> prime(max_val + 1, true); // Remaining part of SIEVE prime[0] = false; prime[1] = false; for (int p = 2; p * p <= max_val; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= max_val; i += p) prime[i] = false; } } int sum = 0; // Traverse through the array for (int i = 0; i < n; ++i) { if (prime[A[i]]) sum += A[i]; } for (int i = 0; i < n; ++i) { if (prime[A[i]] && sum % A[i] == 0) { cout << "YES"; return; } } cout << "NO"; } // Driver program int main() { int A[] = { 1, 2, 3, 4, 5 }; int n = sizeof(A) / sizeof(A[0]); SumDivPrime(A, n); return 0; } |
Java
// Java program to check if sum of primes from an array // is divisible by any of the primes from the same array class Solution { //returns the maximum value static int max_element(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) if(max<A[i]) max=A[i]; return max; } // Function to print "YES" if sum of primes from an array // is divisible by any of the primes from the same array static void SumDivPrime(int A[], int n) { int max_val = (max_element(A)) + 1; // USE SIEVE TO FIND ALL PRIME NUMBERS LESS // THAN OR EQUAL TO max_val // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. boolean prime[]=new boolean[max_val+1]; //initilize the array for(int i=0;i<=max_val;i++) prime[i]=true; // Remaining part of SIEVE prime[0] = false; prime[1] = false; for (int p = 2; p * p <= max_val; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= max_val; i += p) prime[i] = false; } } int sum = 0; // Traverse through the array for (int i = 0; i < n; ++i) { if (prime[A[i]]) sum += A[i]; } for (int i = 0; i < n; ++i) { if (prime[A[i]] && sum % A[i] == 0) { System.out.println( "YES"); return; } } System.out.println("NO"); } // Driver program public static void main(String args[]) { int A[] = { 1, 2, 3, 4, 5 }; int n = A.length; SumDivPrime(A, n); } } //contributed by Arnab Kundu |
Python3
# Python3 program to check if sum of # primes from an array is divisible # by any of the primes from the same array import math # Function to print "YES" if sum of primes # from an array is divisible by any of the # primes from the same array def SumDivPrime(A, n): max_val = max(A) + 1 # USE SIEVE TO FIND ALL PRIME NUMBERS # LESS THAN OR EQUAL TO max_val # Create a boolean array "prime[0..n]". # A value in prime[i] will finally be # false if i is Not a prime, else true. prime = [True] * (max_val + 1) # Remaining part of SIEVE prime[0] = False prime[1] = False for p in range(2, int(math.sqrt(max_val)) + 1): # If prime[p] is not changed, # then it is a prime if prime[p] == True : # Update all multiples of p for i in range(2 * p, max_val + 1, p): prime[i] = False sum = 0 # Traverse through the array for i in range(0, n): if prime[A[i]]: sum += A[i] for i in range(0, n): if prime[A[i]] and sum % A[i] == 0: print("YES") return print("NO") # Driver Code A = [ 1, 2, 3, 4, 5 ] n = len(A) SumDivPrime(A, n) # This code is contributed # by saurabh_shukla |
C#
// C# program to check if sum of primes // from an array is divisible by any of // the primes from the same array class GFG { //returns the maximum value static int max_element(int[] A) { int max = System.Int32.MinValue; for(int i = 0; i < A.Length; i++) if(max < A[i]) max = A[i]; return max; } // Function to print "YES" if sum of // primes from an array is divisible // by any of the primes from the same array static void SumDivPrime(int[] A, int n) { int max_val = (max_element(A)) + 1; // USE SIEVE TO FIND ALL PRIME NUMBERS LESS // THAN OR EQUAL TO max_val // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. bool[] prime=new bool[max_val+1]; //initilize the array for(int i = 0; i <= max_val; i++) prime[i] = true; // Remaining part of SIEVE prime[0] = false; prime[1] = false; for (int p = 2; p * p <= max_val; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= max_val; i += p) prime[i] = false; } } int sum = 0; // Traverse through the array for (int i = 0; i < n; ++i) { if (prime[A[i]]) sum += A[i]; } for (int i = 0; i < n; ++i) { if (prime[A[i]] && sum % A[i] == 0) { System.Console.WriteLine( "YES"); return; } } System.Console.WriteLine("NO"); } // Driver code public static void Main() { int []A = { 1, 2, 3, 4, 5 }; int n = A.Length; SumDivPrime(A, n); } } // This code is contributed by mits |
PHP
<?php // PHP program to check if sum of primes // from an array is divisible by any of // the primes from the same array // Function to print "YES" if sum of primes // from an array is divisible by any of the // primes from the same array function SumDivPrime($A, $n) { $max_val = max($A); // USE SIEVE TO FIND ALL PRIME NUMBERS // LESS THAN OR EQUAL TO max_val // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. $prime = array_fill(1, $max_val + 1, true); // Remaining part of SIEVE $prime[0] = false; $prime[1] = false; for ($p = 2; $p * $p <= $max_val; $p++) { // If prime[p] is not changed, then // it is a prime if ($prime[$p] == true) { // Update all multiples of p for ($i = $p * 2; $i <= $max_val; $i += $p) $prime[$i] = false; } } $sum = 0; // Traverse through the array for ($i = 0; $i < $n; ++$i) { if ($prime[$A[$i]]) $sum += $A[$i]; } for ($i = 0; $i < $n; ++$i) { if ($prime[$A[$i]] && $sum % $A[$i] == 0) { echo "YES"; return; } } echo "NO"; } // Driver Code $A = array( 1, 2, 3, 4, 5 ); $n = sizeof($A) ; SumDivPrime($A, $n); // This code is contributed by Ryuga ?> |
YES
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:
- Count primes that can be expressed as sum of two consecutive primes and 1
- Count of primes below N which can be expressed as the sum of two primes
- Length of largest sub-array having primes strictly greater than non-primes
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Find the prime numbers which can written as sum of most consecutive primes
- Minimum difference between any two primes from the given range
- Check whether the sum of prime elements of the array is prime or not
- Count the number of primes in the prefix sum array of the given array
- Rearrange the array to maximize the number of primes in prefix sum of the array
- Prime points (Points that split a number into two primes)
- K-Primes (Numbers with k prime factors) in a range
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Check if an integer can be expressed as a sum of two semi-primes
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Check if LCM of array elements is divisible by a prime number or not
- Check if a prime number can be expressed as sum of two Prime Numbers
- Elements of an array that are not divisible by any element of another array
- Check if any permutation of N equals any power of K
- Check if any permutation of a number without any leading zeros is a power of 2 or not
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.

