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 < nsuch that nums[i] == nums[j] and (i * j) is divisible by k.

Example 1:

Input: nums = [3,1,2,2,2,1,3], k = 2
Output: 4
Explanation:
There are 4 pairs that meet all the requirements:
- nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.
- nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.
- nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.
- nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.

Example 2:

Input: nums = [1,2,3,4], k = 1
Output: 0
Explanation: Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i], k <= 100

Approach 01:

#include <vector>
#include <unordered_map>
using namespace std;

class Solution {
public:
    int countPairs(vector<int>& nums, int k) {
        unordered_map<int, vector<int>> numberToIndices;

        for (int index = 0; index < nums.size(); ++index) {
            int number = nums[index];
            numberToIndices[number].push_back(index);
        }

        int validPairCount = 0;
        for (const auto& [number, indices] : numberToIndices) {
            int indexCount = indices.size();
            for (int i = 0; i < indexCount - 1; ++i) {
                for (int j = i + 1; j < indexCount; ++j) {
                    if ((indices[i] * indices[j]) % k == 0) {
                        ++validPairCount;
                    }
                }
            }
        }

        return validPairCount;
    }
};
from typing import List
from collections import defaultdict

class Solution:
    def countPairs(self, nums: List[int], k: int) -> int:
        numberToIndices = defaultdict(list)

        for index, number in enumerate(nums):
            numberToIndices[number].append(index)

        validPairCount = 0
        for indices in numberToIndices.values():
            indexCount = len(indices)
            for i in range(indexCount - 1):
                for j in range(i + 1, indexCount):
                    if (indices[i] * indices[j]) % k == 0:
                        validPairCount += 1

        return validPairCount

Time Complexity:

  • Mapping Numbers to Indices:

    Iterates through the array once to build the hash map, which takes \( O(n) \) time.

  • Counting Valid Pairs:

    For each list of indices with the same number, we check all pairs, which takes \( O(m^2) \) per group where \( m \) is the number of indices.

    In the worst case (all elements are the same), this becomes \( O(n^2) \).

  • Total Time Complexity:

    \( O(n^2) \) in the worst case.

Space Complexity:

  • Hash Map Storage:

    Stores each number and its list of indices, taking up to \( O(n) \) space.

  • Total Space Complexity:

    \( O(n) \)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top