Skip to main content
Back to problems
#2176
Easy Algorithms

Count equal and divisible pairs in an array

Array
84.0% acceptance
Feb 25, 2026
1038
69
Given a 0-indexed integer array nums and an integer k, return the number of pairs (i, j) where 0 <= i < j < n, such that nums[i] == nums[j] and (i * j) is divisible by k.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_pairs(nums: Vec<i32>, k: i32) -> i32 {
    let n = nums.len();
    let mut count = 0;
    for i in 0..n {
      for j in i + 1..n {
        if nums[i] == nums[j] && (i * j) as i32 % k == 0 {
          count += 1;
        }
      }
    }
    count
  }
}