2176. Count Equal and Divisible Pairs in an Array LeetCode Solution

In this guide, you will get 2176. Count Equal and Divisible Pairs in an Array LeetCode Solution with the best time and space complexity. The solution to Count Equal and Divisible Pairs in an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Equal and Divisible Pairs in an Array solution in C++
  4. Count Equal and Divisible Pairs in an Array solution in Java
  5. Count Equal and Divisible Pairs in an Array solution in Python
  6. Additional Resources
2176. Count Equal and Divisible Pairs in an Array LeetCode Solution image

Problem Statement of 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 < n, such 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

Complexity Analysis

  • Time Complexity: O(n\sqrt{k})
  • Space Complexity: O(n)

2176. Count Equal and Divisible Pairs in an Array LeetCode Solution in C++

class Solution {
 public:
  int countPairs(vector<int>& nums, int k) {
    int ans = 0;
    unordered_map<int, vector<int>> numToIndices;

    for (int i = 0; i < nums.size(); ++i)
      numToIndices[nums[i]].push_back(i);

    for (const auto& [_, indices] : numToIndices) {
      unordered_map<int, int> gcds;
      for (const int i : indices) {
        const int gcd_i = gcd(i, k);
        for (const auto& [gcd_j, count] : gcds)
          if (gcd_i * gcd_j % k == 0)
            ans += count;
        ++gcds[gcd_i];
      }
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2176. Count Equal and Divisible Pairs in an Array LeetCode Solution in Java

class Solution {
  public int countPairs(int[] nums, int k) {
    int ans = 0;
    Map<Integer, List<Integer>> numToIndices = new HashMap<>();

    for (int i = 0; i < nums.length; ++i) {
      numToIndices.putIfAbsent(nums[i], new ArrayList<>());
      numToIndices.get(nums[i]).add(i);
    }

    for (List<Integer> indices : numToIndices.values()) {
      Map<Integer, Integer> gcds = new HashMap<>();
      for (final int i : indices) {
        final int gcd_i = gcd(i, k);
        for (final int gcd_j : gcds.keySet())
          if (gcd_i * gcd_j % k == 0)
            ans += gcds.get(gcd_j);
        gcds.merge(gcd_i, 1, Integer::sum);
      }
    }

    return ans;
  }

  private int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
  }
}
// code provided by PROGIEZ

2176. Count Equal and Divisible Pairs in an Array LeetCode Solution in Python

class Solution:
  def countPairs(self, nums: list[int], k: int) -> int:
    ans = 0
    numToIndices = collections.defaultdict(list)

    for i, num in enumerate(nums):
      numToIndices[num].append(i)

    for indices in numToIndices.values():
      gcds = collections.Counter()
      for i in indices:
        gcd_i = math.gcd(i, k)
        for gcd_j, count in gcds.items():
          if gcd_i * gcd_j % k == 0:
            ans += count
        gcds[gcd_i] += 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.