Data Structures & AlgorithmsHard
How many contiguous subarrays of 3 5 2 6 1 7 4 8 with length between 2 and 4 inclusive have a sum divisible by 4?
- A. 8
- B. 15
- C. 5
- D. 12
Correct answer: A. 8
Restrict first to blocks of length 2 to 4, then test the sum modulo 4; both conditions apply to each block. Dropping the length bound, shrinking it, or testing divisibility by 2 gives other totals.
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
- How many NON-EMPTY subsets of {1, 2, 3, 4, 5, 6} have a sum that is a prime number?
- How many index pairs i < j in 1 2 4 8 9 3 12 6 have a product A[i]·A[j] that is a perfect square?
- How many index triples i < j < k in 2 5 3 8 6 9 4 have A[i] < A[j] < A[k] AND an EVEN total sum?
- For counting subsequences equal to a fixed pattern (like 'abc'), why must the DP update the longer prefixes first?
- Why does the count of subsequences (index subsets) with sum divisible by m use a DP over residues rather than enumerating subsets?
- Why is an XOR-zero subset family structured like a vector space over the two-element field?