Data Structures & AlgorithmsHard
What is the sum of the sums of all contiguous subarrays of [999999999, 888888888, 777777777], taken modulo 1000000007?
- A. 888888824
- B. 333333309
- C. 999999895
- D. 8888888880
Correct answer: A. 888888824
Element a[i] (0-based) appears in (i+1)(n−i) subarrays, so the total is Σ a[i]·(i+1)·(n−i). The zero-based formula a[i]·i·(n−i) misses the element itself, and skipping the modulus gives a different number entirely.
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 is the sum, over all contiguous subarrays of [3, 1, 3, 2], of the maximum element of each subarray?
- In [4, 7, 5, 9, 6, 3], how many contiguous subarrays have (max − min) ≤ 3?
- How many contiguous subarrays of [1, 2, 1, 3, 3, 2, 2, 1] contain at most 2 distinct values?
- For the array [1, 3, 2, 6, 7, 4, 8, 12, 5], how many index pairs i < j have (J[i] + J[j]) divisible by 4?
- In the binary array [1, 0, 0, 1, 0, 1, 1, 0, 0, 0], how many contiguous subarrays contain equal numbers of 0s and 1s?
- What is the length of the longest contiguous subarray of [2, -4, 3, 1, 6, -2, 5, 4] whose sum is divisible by 6?