Last Updated: 8 Dec, 2025

Prime Sum

Moderate

Problem statement

You are given an array of n integers. Your task is to find the minimum non-negative integer that must be added to the array to make the total sum of all its elements a prime number.


The process is as follows:

1) Calculate the sum of all elements currently in the array.
2) Check if this sum is a prime number.
3) If the sum is already prime, no number needs to be added, so the answer is 0.
4) If the sum is not prime, find the smallest prime number that is strictly greater than the current sum. The difference between this next prime and the current sum is the minimum number you need to add.


Input Format:
The first line contains a single integer N, the number of elements in the array.

The second line contains N space-separated integers, representing the elements of the array.


Output Format:
Your function should return a single integer.


Note:
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The number 1 is not a prime number.

The sum of the array can be large, so you will need an efficient way to find the next prime number.