Data Structures & AlgorithmsHard
How many contiguous subarrays of [1, 2, 3, 4, 5, 6, 7, 8] have a sum that is a prime number?
- A. 9
- B. 27
- C. 20
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- For each contiguous window of length 3 in [1, 2, 1, 3, 2, 1], count the number of distinct values inside it, then add these counts over…
- The numbers 1, 2, 3, 4, 5 are pushed onto an initially empty stack in this order, and a pop may be performed whenever the stack is…
- What is the minimum number of positive integers (each value at most 10, repetitions allowed in the array) needed so that EVERY integer…
- Given a sorted array, why is counting triplets with sum < T O(n²) rather than O(n³)?
- Floyd's cycle detection finds the slow and fast pointers meeting inside the loop. How is the START of the loop then found?
- In the greedy patching algorithm, the reachable range is [1..R] and the next sorted value x exceeds R+1. Why is R+1 the right patch?