#C11335. Split Array into Three Prime Subarray Sums
Split Array into Three Prime Subarray Sums
Split Array into Three Prime Subarray Sums
You are given an array of n integers. Your task is to determine whether it is possible to split the given array into exactly three contiguous subarrays such that the sum of the values in each subarray is a prime number.
Let the three subarrays be defined by indices split at positions i and j, with 1 ≤ i < j < n. The sums of the subarrays will be:
- \( S_1 = \sum_{k=1}^{i} a_k \)
- \( S_2 = \sum_{k=i+1}^{j} a_k \)
- \( S_3 = \sum_{k=j+1}^{n} a_k \)
Your program should output YES
if there exists a valid split such that \( S_1 \), \( S_2 \), and \( S_3 \) are all prime numbers, and NO
otherwise.
Note: A prime number is defined as a natural number greater than 1 that has no positive divisors other than 1 and itself.
inputFormat
The first line of input contains an integer n (the number of elements in the array).
The second line contains n space-separated integers representing the array elements.
Example:
8 2 4 5 1 2 7 3 6
outputFormat
Output a single line containing either YES
if the array can be split into three contiguous subarrays with prime sums, or NO
if it cannot.
Example:
NO## sample
8
2 4 5 1 2 7 3 6
NO