Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) such that:
0 <= i < j <= n - 1 andnums[i] * nums[j] is divisible by k.Example 1:
Input: nums = [1,2,3,4,5], k = 2 Output: 7 Explanation: The 7 pairs of indices whose corresponding products are divisible by 2 are (0, 1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4). Their products are 2, 4, 6, 8, 10, 12, and 20 respectively. Other pairs such as (0, 2) and (2, 4) have products 3 and 15 respectively, which are not divisible by 2.
Example 2:
Input: nums = [1,2,3,4], k = 5 Output: 0 Explanation: There does not exist any pair of indices whose corresponding product is divisible by 5.
Constraints:
1 <= nums.length <= 1051 <= nums[i], k <= 105Problem summary: Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) such that: 0 <= i < j <= n - 1 and nums[i] * nums[j] is divisible by k.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[1,2,3,4,5] 2
[1,2,3,4] 5
number-of-single-divisor-triplets)check-if-array-pairs-are-divisible-by-k)find-the-number-of-good-pairs-ii)find-the-number-of-good-pairs-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2183: Count Array Pairs Divisible by K
class Solution {
public long countPairs(int[] nums, int k) {
long ans = 0;
Map<Integer, Integer> gcds = new HashMap<>();
for (final int num : nums) {
final int gcd_i = gcd(num, k);
for (final int gcd_j : gcds.keySet())
if ((long) gcd_i * gcd_j % k == 0)
ans += gcds.get(gcd_j);
gcds.merge(gcd_i, 1, Integer::sum);
}
return ans;
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
// Accepted solution for LeetCode #2183: Count Array Pairs Divisible by K
// Auto-generated Go example from java.
func exampleSolution() {
}
// Reference (java):
// // Accepted solution for LeetCode #2183: Count Array Pairs Divisible by K
// class Solution {
// public long countPairs(int[] nums, int k) {
// long ans = 0;
// Map<Integer, Integer> gcds = new HashMap<>();
//
// for (final int num : nums) {
// final int gcd_i = gcd(num, k);
// for (final int gcd_j : gcds.keySet())
// if ((long) gcd_i * gcd_j % k == 0)
// ans += gcds.get(gcd_j);
// gcds.merge(gcd_i, 1, Integer::sum);
// }
//
// return ans;
// }
//
// private int gcd(int a, int b) {
// return b == 0 ? a : gcd(b, a % b);
// }
// }
# Accepted solution for LeetCode #2183: Count Array Pairs Divisible by K
class Solution:
def countPairs(self, nums: list[int], k: int) -> int:
ans = 0
gcds = collections.Counter()
for num in nums:
gcd_i = math.gcd(num, k)
for gcd_j, count in gcds.items():
if gcd_i * gcd_j % k == 0:
ans += count
gcds[gcd_i] += 1
return ans
// Accepted solution for LeetCode #2183: Count Array Pairs Divisible by K
/**
* [2183] Count Array Pairs Divisible by K
*
* Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) such that:
*
* 0 <= i < j <= n - 1 and
* nums[i] * nums[j] is divisible by k.
*
*
* Example 1:
*
* Input: nums = [1,2,3,4,5], k = 2
* Output: 7
* Explanation:
* The 7 pairs of indices whose corresponding products are divisible by 2 are
* (0, 1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4).
* Their products are 2, 4, 6, 8, 10, 12, and 20 respectively.
* Other pairs such as (0, 2) and (2, 4) have products 3 and 15 respectively, which are not divisible by 2.
*
* Example 2:
*
* Input: nums = [1,2,3,4], k = 5
* Output: 0
* Explanation: There does not exist any pair of indices whose corresponding product is divisible by 5.
*
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* 1 <= nums[i], k <= 10^5
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/count-array-pairs-divisible-by-k/
// discuss: https://leetcode.com/problems/count-array-pairs-divisible-by-k/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn count_pairs(nums: Vec<i32>, k: i32) -> i64 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2183_example_1() {
let nums = vec![1, 2, 3, 4, 5];
let k = 2;
let result = 7;
assert_eq!(Solution::count_pairs(nums, k), result);
}
#[test]
#[ignore]
fn test_2183_example_2() {
let nums = vec![1, 2, 3, 4];
let k = 5;
let result = 0;
assert_eq!(Solution::count_pairs(nums, k), result);
}
}
// Accepted solution for LeetCode #2183: Count Array Pairs Divisible by K
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2183: Count Array Pairs Divisible by K
// class Solution {
// public long countPairs(int[] nums, int k) {
// long ans = 0;
// Map<Integer, Integer> gcds = new HashMap<>();
//
// for (final int num : nums) {
// final int gcd_i = gcd(num, k);
// for (final int gcd_j : gcds.keySet())
// if ((long) gcd_i * gcd_j % k == 0)
// ans += gcds.get(gcd_j);
// gcds.merge(gcd_i, 1, Integer::sum);
// }
//
// return ans;
// }
//
// private int gcd(int a, int b) {
// return b == 0 ? a : gcd(b, a % b);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.