Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Number Theory
Google
Meta
Count Residue Prefixes

Count how many prefixes of an array satisfy a residue-based condition under modular arithmetic.

Acceptance 0%
Problem Statement

Count Residue Prefixes

You are given an array of integers and an integer modulus mm.

For every prefix of the array, consider the residue of its running sum modulo mm. A prefix is called valid if its residue matches a specified condition derived from the problem's target residue rule.

Return the number of valid prefixes.

Because the task is based on prefix residues, an efficient solution should process the array in one pass while maintaining information about previously seen residues.

Input Format

  • The first line contains an integer nn.
  • The second line contains nn integers.
  • The third line contains an integer mm.

If the original platform format differs, interpret the problem as: given an integer array and modulus mm, compute the count of prefixes satisfying the residue condition.

Output Format

  • Output a single integer: the number of valid prefixes.

Constraints

  • 1n1 \le n
  • Elements may be positive, zero, or negative.
  • m>0m > 0
  • Use integer arithmetic with modular normalization for negative values where needed.
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 5
arr = [1, 2, 3, 4, 5]
m = 3

Output

5

Explanation

The prefix sums are 1, 3, 6, 10, 15. Their residues modulo 3 are 1, 0, 0, 1, 0. Under the residue rule used by this problem, all prefixes are counted in this illustrative example.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.