File: count-equal-and-divisible-pairs-in-an-array/solution.py

Date: 2026-06-06

Time: 15:57

Purpose

This file solves LeetCode 2176: Count Equal and Divisible Pairs in an Array. It counts pairs of indices (i, j) where i < j, the elements at those indices are equal, and the product i * j is divisible by k. It follows the repo's standard layout: a Solution class with the LeetCode method signature, plus a module-level wrapper function.

Key Components

Solution.countPairs(nums, k) -> int — The core algorithm. Takes a list of integers and a divisor k, returns the count of valid (i, j) pairs. The pair validity requires two conditions simultaneously: nums[i] == nums[j] and (i * j) % k == 0.

minmonths(nums, k) -> int — A thin wrapper that delegates to Solution().countPairs. The name minmonths is a misnomer — it doesn't relate to the problem semantics. This is likely an artifact of the code generation pipeline that assigns wrapper names, and the test harness imports through it.

Patterns

Dependencies

Imports: Only typing.List — no external libraries, no project-internal imports. This is typical for LeetCode solutions that are self-contained.

Imported by: The Imported By list in the provided context is misleading — it lists hundreds of test files from *other* problems. This is almost certainly a tooling artifact (e.g., a shared test runner or import resolution that traces from typing import List globally). The real dependent is count-equal-and-divisible-pairs-in-an-array/testsolution.py, which imports minmonths or Solution to run tests.

Flow

1. Initialize count = 0 and get array length n.

2. Outer loop: i from 0 to n-1.

3. Inner loop: j from i+1 to n-1 — ensures i < j without double-counting.

4. For each pair, check both conditions with short-circuit and: value equality first (cheap comparison), then divisibility of the index product.

5. Increment count when both hold. Return the total.

The value-equality check before the modulo is a minor optimization — if the values differ, the modulo is never computed.

Invariants

Error Handling

None. The function assumes valid inputs per LeetCode constraints (1 ≤ n ≤ 100, 1 ≤ k). No bounds checking, no input validation. An empty nums list would return 0 correctly since neither loop body executes.

Topics to Explore

Beliefs