{x}
blog image

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

Solution Explanation:

This problem asks us to count the number of pairs (i, j) in an array nums such that nums[i] == nums[j] and i * j is divisible by k. The solution uses a brute-force approach with nested loops.

Approach:

  1. Nested Loops: The core of the solution is a pair of nested loops. The outer loop iterates through each element of the array from index 1 (since we need j > i). The inner loop iterates through elements from index 0 up to the current index of the outer loop (j).

  2. Equality Check: Inside the inner loop, it checks if nums[i] is equal to nums[j]. If they are not equal, the inner loop continues to the next iteration.

  3. Divisibility Check: If nums[i] and nums[j] are equal, the code checks if the product i * j is divisible by k using the modulo operator (%). If the remainder is 0, it means the product is divisible by k.

  4. Increment Counter: If both the equality and divisibility checks pass, it increments the ans counter, which keeps track of the number of pairs satisfying the conditions.

  5. Return Counter: Finally, the function returns the value of ans, representing the total count of pairs.

Time Complexity Analysis:

The nested loops iterate through all possible pairs of indices (i, j) where 0 <= i < j < n, resulting in O(n²) comparisons. The operations inside the loops (comparisons and modulo operation) take constant time O(1). Therefore, the overall time complexity of this solution is O(n²), where n is the length of the input array nums.

Space Complexity Analysis:

The solution uses only a few variables (ans, i, j, x, y) to store intermediate values. The space used does not depend on the size of the input array. Thus, the space complexity is O(1), which is constant space.

Code Examples (with detailed comments):

Python:

class Solution:
    def countPairs(self, nums: List[int], k: int) -> int:
        ans = 0  # Initialize the counter for valid pairs
        n = len(nums) # Get the length of the array for efficiency
        for j in range(1, n): # Outer loop starts from index 1
            for i in range(j): # Inner loop iterates from 0 to j-1
                if nums[i] == nums[j] and (i * j) % k == 0: # Check both conditions
                    ans += 1 # Increment the counter if both conditions are true
        return ans
 

Java:

class Solution {
    public int countPairs(int[] nums, int k) {
        int ans = 0; // Initialize the counter
        for (int j = 1; j < nums.length; ++j) { // Outer loop
            for (int i = 0; i < j; ++i) { // Inner loop
                if (nums[i] == nums[j] && (i * j) % k == 0) { // Check conditions
                    ans++; // Increment counter
                }
            }
        }
        return ans;
    }
}

The other languages (C++, Go, TypeScript, Rust, C) follow a very similar structure, differing only in syntax. They all employ the same nested loop approach with the equality and divisibility checks, leading to the same time and space complexities.