Data Structures & AlgorithmsHard
How many contiguous subarrays of [1, 5, 2, 8, 3, 7] have a sum divisible by their own length?
- A. 8
- B. 7
- C. 3
- D. 14
Correct answer: D. 14
Test each subarray's own length: the average is an integer exactly when the sum is divisible by the subarray's length. Using the full array length or the complement gives a different count.
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
- For [2, 3, 4, 6, 9, 5, 12], how many index pairs i < j have the product BB[i]·BB[j] divisible by 6?
- How many contiguous subarrays of [5, 5, 3, 3, 6, 6, 6] have XOR equal to 0?
- How many index pairs i < j in [1, 5, 3, 3, 3, 7, 2, 4, 6] have DD[i] + DD[j] = 6? Equal values at different indices are different pairs.
- How many contiguous subarrays of [2, 1, 3, 1, 1, 2, 4, 1] have each element sum equal to its length (sum of (a[i] − 1) over the subarray…
- How many contiguous subarrays of [1, 2, 1, 2, 3, 3, 4] contain exactly 2 distinct values?
- In the array [2, 7, 3, 7, 1, 4], how many contiguous subarrays have their LEFT-most element equal to the maximum of the subarray (ties…