Count Equal and Divisible Pairs in an Array
Intuition\n Describe your first thoughts on how to solve this problem. \nThe task is to count pairs in an array where the elements are equal and the product of 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe task is to count pairs in an array where the elements are equal and the product of their indices is divisible by a given integer `k`. The simplest way to achieve this is to use a brute-force approach where we check all possible pairs of indices and apply the given conditions.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Special Case for `k = 0`**: \n   - If `k` is 0, the code returns 1 immediately. This might not make logical sense in general contexts (since checking `num % k` when `k = 0` would cause an error), but as per the code provided, we return 1 directly for `k = 0`.\n\n2. **Brute-force Pair Checking**:\n   - Initialize a counter `count` to zero.\n   - Use two nested loops to iterate over all possible pairs `(i, j)` where `i < j`.\n   - For each pair `(i, j)`, check if `nums[i]` is equal to `nums[j]`.\n   - If they are equal, compute the product of their indices `i * j`.\n   - Check if this product is divisible by `k`. If it is, increment the `count`.\n\n3. **Return the Result**:\n   - After checking all pairs, return the `count`.\n\n# Complexity\n- **Time Complexity**:\n  - The time complexity of this approach is (O(n^2)), where (n) is the length of the array. This is because we are using a nested loop to check all pairs of indices.\n\n- **Space Complexity**:\n  - The space complexity is (O(1)) because we are using a fixed amount of additional space regardless of the input size.\n\n# Code\n\n```java\nclass Solution {\n    public int countPairs(int[] nums, int k) {\n        // Special case when k is 0\n        if (k == 0) {\n            return 1;\n        }\n\n        int count = 0;\n        // Brute-force approach: Check all pairs (i, j)\n        for (int i = 0; i < nums.length; i++) {\n            for (int j = i + 1; j < nums.length; j++) {\n                // Check if elements at index i and j are equal\n                if (nums[i] == nums[j]) {\n                    int num = i * j;\n                    // Check if the product of indices is divisible by k\n                    if (num % k == 0) {\n                        count++;\n                    }\n                }\n            }\n        }\n        // Return the count of valid pairs\n        return count;   \n    }\n}\n```\n\n# Example Walkthrough\nFor `nums = [1, 2, 3, 1, 1, 3]` and `k = 2`:\n1. The pairs of indices `(i, j)` where `nums[i] == nums[j]` are `(0, 3)`, `(0, 4)`, `(3, 4)`, and `(2, 5)`.\n2. Their corresponding products are `0*3 = 0`, `0*4 = 0`, `3*4 = 12`, and `2*5 = 10`.\n3. Among these, the products `0`, `0`, and `12` are divisible by `2`, so the count is `3`.
['Array', 'Java']