[LeetCode] Count Equal and Divisible Pairs in an Array

2176. Count Equal and Divisible Pairs in an Array

Given a 0-indexed integer array nums of length n 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int countPairs(vector<int>& nums, int k) {
int res = 0;
for(int i = 0; i < nums.size(); i++) {
for(int j = i + 1; j < nums.size(); j++) {
if(nums[i] == nums[j] and i * j % k == 0)
res++;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/20/PS/LeetCode/count-equal-and-divisible-pairs-in-an-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.