Data Structures & AlgorithmsModerate
How many contiguous subarrays of 5 2 7 1 8 3 6 4 have an ODD length and a sum divisible by 7?
- A. 5
- B. 3
- C. 2
- D. 17
Correct answer: B. 3
Both conditions apply to the same block: the length parity and the sum residue mod 7. Dropping the parity test, reversing it, or negating the residue test each counts a different family.
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
- What is the sum, over all non-empty contiguous subarrays of 5 3 6 1 4, of their XOR values, taken modulo 1000?
- How many arrays of length 3 with entries in {0, 1, 2, 3} have a sum divisible by 4?
- How many ORDERED pairs (i, j) with i ≠ j in 1 4 6 9 10 3 5 satisfy that A[i] + A[j] is divisible by 5?
- How many contiguous subarrays of 1 3 5 2 4 6 1 have a sum that is a perfect square (0 is not present here)?
- How many split points i (0 ≤ i ≤ n−2) of 2 5 1 4 3 6 2 7 satisfy: (sum of A[0..i]) mod 3 equals (sum of A[i+1..n−1]) mod 3?
- How many contiguous subarrays of 3 1 2 6 4 5 9 2 1 have a length divisible by 3 AND a sum divisible by 3?