Data Structures & AlgorithmsVery hardGATE CS · DSA
How many contiguous subarrays of [−2, 1, −3, −4, 8, 1, 4, −5, 6] have a sum divisible by 3? (Count equal prefix-sum remainders.)
- A. 11
- B. 13
- C. 8
- D. 12
Correct answer: D. 12
Two prefixes with the same remainder bound a subarray whose sum is divisible by 3, so add C(f, 2) over remainder classes (including prefix 0). Normalising negative remainders matters: in C/Java, −1 % 3 is −1, which must be mapped to 2; done right the count is 12.
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 does this loop-counting code print? (Python) python n, c = 16, 0 i = 2 while i <= n: c += 1 i = i i print(c)
- An equilibrium index i has (sum of elements left of i) = (sum of elements right of i), excluding a[i]. Which indices of [0, 9, 2, 3, 4, 2…
- A stack is implemented with two queues where push is costly (new element goes into the empty queue, then everything else is moved behind…
- Daily prices are [15, 17, 19, 13, 20, 17, 38, 38, 40]. What is the maximum profit with at most ONE buy and one later sell, and with…
- Using a sliding window with a last-seen index map, what is the length of the longest substring of "dbdcccdcbaddad" with all characters…
- Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version…