2183. Count Array Pairs Divisible by K LeetCode Solution

In this guide, you will get 2183. Count Array Pairs Divisible by K LeetCode Solution with the best time and space complexity. The solution to Count Array Pairs Divisible by K 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 Array Pairs Divisible by K solution in C++
  4. Count Array Pairs Divisible by K solution in Java
  5. Count Array Pairs Divisible by K solution in Python
  6. Additional Resources
2183. Count Array Pairs Divisible by K LeetCode Solution image

Problem Statement of Count Array Pairs Divisible by K

Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) such that:

0 <= i < j <= n – 1 and
nums[i] * nums[j] is divisible by k.

Example 1:

Input: nums = [1,2,3,4,5], k = 2
Output: 7
Explanation:
The 7 pairs of indices whose corresponding products are divisible by 2 are
(0, 1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4).
Their products are 2, 4, 6, 8, 10, 12, and 20 respectively.
Other pairs such as (0, 2) and (2, 4) have products 3 and 15 respectively, which are not divisible by 2.

Example 2:

Input: nums = [1,2,3,4], k = 5
Output: 0
Explanation: There does not exist any pair of indices whose corresponding product is divisible by 5.

Constraints:

1 <= nums.length <= 105
1 <= nums[i], k <= 105

Complexity Analysis

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

2183. Count Array Pairs Divisible by K LeetCode Solution in C++

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

    for (const int num : nums) {
      const int gcd_i = gcd(num, k);
      for (const auto& [gcd_j, count] : gcds)
        if (static_cast<long>(gcd_i) * gcd_j % k == 0)
          ans += count;
      ++gcds[gcd_i];
    }

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

2183. Count Array Pairs Divisible by K LeetCode Solution in Java

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

    for (final int num : nums) {
      final int gcd_i = gcd(num, k);
      for (final int gcd_j : gcds.keySet())
        if ((long) 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

2183. Count Array Pairs Divisible by K LeetCode Solution in Python

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

    for num in nums:
      gcd_i = math.gcd(num, 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.