🎖️ Day 5: Subarray Divisibility

easysliding window

Given an array of $$$n$$$ integers, determine the number of subarrays whose sum is divisible by $$$n$$$.

A subarray is a contiguous part of the array. Formally, for indices $$$1 \leq l \leq r \leq n$$$, the subarray $$$a[l \dots r]$$$ consists of elements $$$a_l, a_{l+1}, \dots, a_r$$$.

Input Format:

The first line contains an integer $$$n$$$ $$$(1 \leq n \leq 2 \cdot 10^5)$$$: the size of the array.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$: the contents of the array.

Output Format:

Print one integer: the required number of subarrays.

Examples:

Example 1:

Input:

5
3 1 2 7 5

Output:

3

Note:

The good subarrays are:

  • $$$[1, 2, 7]$$$: The sum is $$$1 + 2 + 7 = 10$$$.
  • $$$[1, 2, 7, 5]$$$: The sum is $$$1 + 2 + 7 + 5 = 15$$$.
  • $$$[5]$$$: The sum is $$$5$$$.
The total count of good subarrays is 3.

Code

Loading Editor...