Data Structures & AlgorithmsHard
How many NON-EMPTY prefixes of 3 2 5 1 4 5 2 (the first k elements, k ≥ 1) have a sum divisible by 5?
- A. 0
- B. 5
- C. 4
- D. 2
Correct answer: C. 4
A prefix is determined by its end index, so test the running sum at each step. Suffixes, an off-by-one that includes the empty prefix, or single elements divisible by 5 each count something else.
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 contiguous subarrays of length exactly 4 in 9 7 8 5 4 2 3 1 are STRICTLY decreasing?
- How many contiguous subarrays of 2 3 5 4 1 7 6 have a PRIME sum and an EVEN length?
- How many contiguous subarrays of length exactly 2 in 2 6 3 5 8 1 4 7 have a sum that is a multiple of 4 OR a product that is a multiple of…
- How many ways can three DIFFERENT sticks of lengths 2 3 4 5 6 7 (chosen by position) form a non-degenerate triangle? A triangle needs the…
- How many index quadruples i < j < k < l in 1 -1 2 -2 3 -3 have A[i] + A[j] + A[k] + A[l] = 0?
- How many index pairs i < j in 4 9 5 3 6 6 10 satisfy |A[i] − A[j]| = 1?