Loading...
You are given a list of integers nums.
nums
A subarray is a non-empty run of consecutive elements of nums. A prime number is an integer greater than 1 whose only divisors are 1 and itself.
Return the largest sum of a subarray whose first element and last element are both prime.
A subarray with one element counts if that element is prime. If there's no such subarray, return -1.
-1
1 <= nums.length <= 10^5
-10^5 <= nums[i] <= 10^5
The whole list starts with 2 and ends with 5, both prime, so the sum is 2 + 4 + 3 + 10 + 5 = 24.
None of the elements is prime, so no subarray qualifies.
Only 7 is prime, so the only valid subarray is [7], with sum 7.
[7]