Count Equal and Divisible Pairs in Array

EasyArrayMath

Description

Given a 0-indexed integer array nums and an integer k, return the number of pairs (i, j) where 0 <= i < j < n, nums[i] == nums[j], and (i * j) is divisible by k.

Examples

Input:nums = [3,1,2,2,2,1,3], k = 2
Output:4
Explanation:

Pairs with equal values where (i*j) % 2 == 0: (0,6) both 3 with 0*6%2=0, (2,3) both 2 with 2*3%2=0, (2,4) both 2 with 2*4%2=0, (3,4) both 2 with 3*4%2=0. Total: 4 valid pairs.

Input:nums = [1,2,3,4], k = 1
Output:0
Explanation:

All elements are distinct, so no pair (i, j) has nums[i] == nums[j]. The count is 0.

Input:nums = [5,5,5], k = 3
Output:3
Explanation:

All elements are 5, so all pairs have equal values. Pairs: (0,1) with 0*1%3=0, (0,2) with 0*2%3=0, (1,2) with 1*2%3=2. Only pairs (0,1) and (0,2) satisfy the divisibility condition. Count: 2.

Constraints

  • 1 ≤ nums.length ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!