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

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

```

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

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 print Yes otherwise No.

Below is the implementation of the above approach:

C++

 `// C++ implementation of the above approach ` `#include ` `#define ll long long int ` `#define MAX 100000 ` `using` `namespace` `std; ` `bool` `prime[MAX]; ` ` `  `// Sieve to find prime ` `void` `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 not ` `bool` `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 code ` `int` `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

 `// Java implementation of the above approach ` `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `static` `int` `MAX =``100000``; ` ` `  `static` `boolean` `prime[] = ``new` `boolean``[MAX]; ` ` `  `// Sieve to find prime ` `static` `void` `sieve() ` `{ ` `    ``for``(``int` `i=``0``;i

Python3

 `# Python3 implementation of above approach ` `from` `math ``import` `gcd, sqrt ` ` `  `MAX` `=` `100000` ` `  `prime ``=` `[``True``] ``*` `MAX` ` `  `# Sieve to find prime ` `def` `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 not ` `def` `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 code ` `if` `__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#

 `// C# implementation of the above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `static` `int` `MAX = 100000; ` ` `  `static` `bool``[] prime = ``new` `bool``[MAX]; ` ` `  `// Sieve to find prime ` `static` `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 not ` `static` `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 code ` `public` `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

 ` `

Output:

```Yes
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

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.