Check whether the sum of prime elements of the array is prime or not - GeeksforGeeks

23 Apr, 2021
Given an array having N elements. The task is to check if the sum of prime elements of the array is prime or not.Examples: 
Input: arr[] = {1, 2, 3}
Output: Yes
As there are two primes in the array i.e. 2 and 3. 
So, the sum of prime is 2 + 3 = 5 and 5 is also prime. 

Input: arr[] = {2, 3, 2, 2}
Output: No
Approach: First find prime number up to 10^5 using Sieve. Then iterate over all elements of the array. If the number is prime then add it to sum. And finally, check whether the sum is prime or not. If prime then prints Yes otherwise No.Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h>#define ll long long int#define MAX 100000using namespace std;bool prime[MAX]; // Sieve to find primevoid sieve(){    memset(prime, true, sizeof(prime));    prime[0] = prime[1] = false;    for (int i = 2; i < MAX; i++)        if (prime[i])            for (int j = 2 * i; j < MAX; j += i)                prime[j] = false;              } // Function to check if the sum of// prime is prime or notbool checkArray(int arr[], int n){    // find sum of all prime number    ll sum = 0;    for (int i = 0; i < n; i++)        if (prime[arr[i]])            sum += arr[i];     // if sum is prime    // then return yes    if (prime[sum])        return true;     return false;} // Driver codeint main(){    // array of elements    int arr[] = { 1, 2, 3 };    int n = sizeof(arr) / sizeof(arr[0]);     sieve();     if (checkArray(arr, n))        cout << "Yes";    else        cout << "No";     return 0;}
// Java implementation of the above approachimport java.io.*; class GFG {     static int MAX =100000; static boolean prime[] = new boolean[MAX]; // Sieve to find primestatic void sieve(){    for(int i=0;i<MAX;i++)    {        prime[i] =true;    }    prime[0] = prime[1] = false;    for (int i = 2; i < MAX; i++)        if (prime[i])            for (int j = 2 * i; j < MAX; j += i)                prime[j] = false;              } // Function to check if the sum of// prime is prime or notstatic boolean checkArray(int arr[], int n){    // find sum of all prime number    int sum = 0;    for (int i = 0; i < n; i++)        if (prime[arr[i]])            sum += arr[i];     // if sum is prime    // then return yes    if (prime[sum])        return true;     return false;} // Driver code     public static void main (String[] args) {    // array of elements    int arr[] = { 1, 2, 3 };    int n = arr.length;     sieve();     if (checkArray(arr, n))        System.out.println("Yes");    else         System.out.println("No");     }}// This code is contributed by shs..
# Python3 implementation of above approachfrom math import gcd, sqrt MAX = 100000 prime = [True] * MAX # Sieve to find primedef sieve() :         # 0 and 1 are not prime numbers    prime[0] = False    prime[1] = False         for i in range(2, MAX) :         if prime[i] :            for j in range(2**i, MAX, i) :                prime[j] = False     # Function to check if the sum of# prime is prime or notdef checkArray(arr, n) :     # find sum of all prime number    sum = 0    for i in range(n) :         if prime[arr[i]] :            sum += arr[i]     # if sum is prime    # then return yes    if prime[sum] :        return True     return False # Driver codeif __name__ == "__main__" :     # list of elements    arr = [1, 2, 3]    n = len(arr)     sieve()     if checkArray(arr, n) :        print("Yes")    else :        print("No")         # This code is contributed by ANKITRAI1
// C# implementation of the above approachusing System; class GFG{static int MAX = 100000; static bool[] prime = new bool[MAX]; // Sieve to find primestatic void sieve(){    for(int i = 0; i < MAX; i++)    {        prime[i] = true;    }    prime[0] = prime[1] = false;    for (int i = 2; i < MAX; i++)        if (prime[i])            for (int j = 2 * i;                     j < MAX; j += i)                prime[j] = false;} // Function to check if the sum of// prime is prime or notstatic bool checkArray(int[] arr, int n){    // find sum of all prime number    int sum = 0;    for (int i = 0; i < n; i++)        if (prime[arr[i]])            sum += arr[i];     // if sum is prime    // then return yes    if (prime[sum])        return true;     return false;} // Driver codepublic static void Main (){    // array of elements    int[] arr = new int[] { 1, 2, 3 };    int n = arr.Length;         sieve();         if (checkArray(arr, n))        Console.WriteLine("Yes");    else        Console.WriteLine("No");}} // This code is contributed by mits
<?php// PHP implementation of the// above approach // Sieve to find primefunction sieve(){    $MAX = 100000;    $prime = array($MAX);    for($i = 0; $i < $MAX; $i++)    {        $prime[$i] = true;    }    $prime[0] = $prime[1] = false;    for ($i = 2; $i < $MAX; $i++)        if ($prime[$i])            for ($j = 2 * $i;                 $j < $MAX; $j += $i)                $prime[$j] = false;} // Function to check if the sum of// prime is prime or notfunction checkArray($arr, $n){    $prime = array(100000);         // find sum of all prime number    $sum = 0;    for ($i = 0; $i < $n; $i++)        if ($prime[$arr[$i]])            $sum += $arr[$i];     // if sum is prime    // then return yes    if ($prime[$sum])        return true;     return false;} // Driver code$arr= array(1, 2, 3);$n = sizeof($arr); sieve(); if (checkArray($arr, $n))    echo "Yes";else    echo "No"; // This code is contributed// by Akanksha Rai?>
<script> // JavaScript implementation of the// above approach // function check whether a number// is prime or notfunction isPrime(n){    // Corner case    if (n <= 1)        return 0;       // Check from 2 to n-1    for (let i = 2; i < n; i++)        if (n % i == 0)            return 0;       return 1;} var prime = new Array(5);   // Sieve to find primefunction sieve(){    for(i = 0; i <=5; i++)    {        prime[i] = isPrime(i);    }} // Function to check if the sum of// prime is prime or notfunction checkArray(arr, n){              // find sum of all prime number    sum = 0;    for (i = 0; i <= n; i++)        if (prime[arr[i]])            sum += arr[i];     // if sum is prime    // then return yes    if (sum)        return 1;     return 0;} var arr= [1, 2, 3];n = 3; sieve(); if (checkArray(arr, n))    document.write("Yes");else    document.write("No");  </script>
Yes
 
Time Complexity: O(n * log(log n))
Auxiliary Space: O(MAX)
Shashank12
Mithun Kumar
ankthon
Akanksha_Rai
subhammahato348
akshitsaxenaa09
Prime Number
sieve
Arrays
Mathematical
Arrays
Mathematical
Prime Number
sieve
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Next Greater Element
Window Sliding Technique
Count pairs with given sum
Program to find sum of elements in a given array
Reversal algorithm for array rotation
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7