Data Structures & AlgorithmsHard

How many contiguous subarrays of [1, 2, 3, 4, 5, 6, 7, 8] have a sum that is a prime number?

  1. A. 9
  2. B. 27
  3. C. 20
  4. D. 5

Correct answer: A. 9

Test each subarray sum for primality, remembering that 1 is not prime. The composite count is the complement, parity is only a necessary condition for an odd prime, and removing single elements drops real primes such as 2, 3, 5, 7.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic