Data Structures & AlgorithmsVery hardGATE CS
How many non-empty contiguous subarrays of [5, 2, -3, 8, 6, -9, 4, 7, 1, 3, 12, -5, 6, 2] have a sum divisible by 7?
- A. 10
- B. 12
- C. 15
- D. 8
Correct answer: A. 10
Using prefix sums mod 7 (Python-style non-negative remainders), the number of equal prefix-remainder pairs is 10. Using the language remainder for negatives would mix up classes.
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
- The infix expression ab^c^d-(e+fg)^h/i+j (^ is right-associative with highest precedence, then and /, then + and -, all others…
- For the pattern P = "abaabaabaabab", the KMP failure (prefix) function pi[i] is the length of the longest proper prefix of P[0..i] that is…
- The naive algorithm searches for P = "aaaab" in T = "aaabaaabaaaab": for each shift s = 0, 1, ..., it compares P[0], P[1], ... with T[s]…
- A queue is built from two stacks: an enqueue pushes onto the input stack; a dequeue pops from the output stack, and if the output stack is…
- From the digit string 1432219836475, exactly 5 digits are deleted (the remaining digits keep their order, and leading zeros are not an…
- An array next = [10, 6, 10, 1, 1, 9, 5, 5, 10, 3, 7] defines a linked structure in which the node after index i is next[i]. Floyd's…