Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many contiguous subarrays of [3, 1, 4, 1, 5, 9] have a prime sum?

  1. A. 8
  2. B. 7
  3. C. 5
  4. D. 2

Correct answer: B. 7

Enumerate subarrays and test each sum for primality with trial division up to its square root. Excluding single-element subarrays (second option) changes the count.

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

Exams that ask this topic