Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT
What is the length of the longest contiguous subarray of [5, 3, -2, 8, -4, 6, 1, 7] whose sum is divisible by 5 (the empty sum 0 counts as divisible)?
- A. 6
- B. 5
- C. 3
- D. 7
Correct answer: B. 5
Record the FIRST index where each prefix-sum remainder occurs; a later index with the same remainder closes a divisible subarray, and the longest such gap is the answer. Recording the last index instead gives shorter subarrays.
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 DISTINCT values can the sum of a subset of [1, 2, 4, 8, 3] take? The empty subset (sum 0) is included.
- How many subsets of [2, 5, 3, 7, 1] (chosen by position, empty subset included) have a total sum at most 8?
- How many permutations of 1 to 6 have NO fixed point (that is, p[i] ≠ i for every position i)?
- How many strings of length 6 over {a, b} have NO position i with s[i] = s[i+2] (for every valid i)?
- How many arrays of length 4 with entries from {1, 2, ..., 3} are NON-INCREASING (each entry is at least the next)?
- Daily prices are [3, 2, 6, 5, 0, 3, 8, 1, 9]. You may complete at most 3 buy-then-sell transactions (one share at a time, sell before the…