Data Structures & AlgorithmsHard
What is the length of the longest contiguous subarray of 1 2 3 5 1 2 3 such that the sum of EVERY prefix of that subarray is divisible by 3?
- A. 2
- B. 5
- C. 1
- D. 7
Correct answer: C. 1
Every prefix of the block must have residue zero mod 3, so the first element must already be divisible by 3 and each later step must keep the running sum at residue zero. Requiring only the total sum to be divisible by 3 is a weaker condition.
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
- Why is a stack-based check that tracks TYPE as well as nesting needed for two bracket kinds?
- To count distinct subsequences of a string with repeated letters, which strategy avoids double counting?
- For the 'largest number from concatenation' problem, why does the comparator compare x+y with y+x as strings?
- When choosing non-adjacent positions to maximise a sum, why does the DP keep two states per position?
- Why does the lexicographically largest subsequence after deleting k digits come from a greedy stack?
- In a sliding window tracking the maximum number of vowels among windows of fixed length 3, why is O(1) update per step possible?