Count Equal and Divisible Pairs in an Array - Problem
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](elements are equal)(i * j)is divisible byk
A pair (i, j) satisfies the conditions if both the values at indices i and j are the same, and the product of their indices is divisible by k.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [3,1,2,2,2,1,3], k = 2
›
Output:
4
💡 Note:
Valid pairs: (2,3), (2,4), (3,4), (0,6). All have equal values and index products divisible by 2.
Example 2 — No Valid Pairs
$
Input:
nums = [1,2,3,4], k = 1
›
Output:
0
💡 Note:
No two elements are equal, so no valid pairs exist regardless of divisibility.
Example 3 — All Same Values
$
Input:
nums = [5,5,5,5], k = 3
›
Output:
5
💡 Note:
Equal value pairs: (0,1), (0,2), (0,3), (1,2), (1,3), (2,3). Pairs (0,1), (0,2), (0,3), (1,3), (2,3) have index products 0, 0, 0, 3, 6 respectively, all divisible by 3.
Constraints
- 1 ≤ nums.length ≤ 100
- 1 ≤ nums[i], k ≤ 100
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code