Count Equal and Divisible Pairs

Easy

Description

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, 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:

Valid pairs: (0,6),(2,3),(2,4),(3,4).

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:2
Explanation:

Pairs must satisfy nums[i] == nums[j] and (i*j) % k == 0. All elements are 5, so checking divisibility: (0*1) % 3 = 0 (valid), (0*2) % 3 = 0 (valid), (1*2) % 3 = 2 (invalid). Valid pairs: (0,1) and (0,2).

Constraints

  • 1 ≤ nums.length ≤ 100

Ready to solve this problem?

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