Difficulty: Easy
Very simple solution, we can brute force it. Iterate through all possible combinations and count the number of combinations that is valid.
class Solution(object):
def countPairs(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
res = 0
for i in range(len(nums) - 1):
for j in range(i + 1, len(nums)):
if nums[i] == nums[j] and (i * j) % k == 0:
res += 1
return res
Time Complexity:
O(n^2)
Space Complexity:
O(1)
Time Taken:
52s