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.
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.
Your function should return a single integer.
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.
5
2 4 6 8 1
2
The sum of the array elements is 2 + 4 + 6 + 8 + 1 = 21.
21 is not a prime number (divisible by 3 and 7).
The next prime number greater than 21 is 23.
The number to be added is 23 - 21 = 2
3
1 5 7
0
The sum of the array elements is 1 + 5 + 7 = 13.
13 is a prime number.
Since the sum is already prime, the number to be added is 0.
The expected time complexity is O(N).
1 <= N <= 1000
1 <= array element <= 1000
Time limit: 1 sec