Java | Brute and Optimised Solution
\n1. Brute Force\nclass Solution {\n    public int countPairs(int[] nums, int k) {\n        int count=0;\n        for(int i=0; i<nums.length-1; i++)\n        {\
```\n1. Brute Force\n```class Solution {\n    public int countPairs(int[] nums, int k) {\n        int count=0;\n        for(int i=0; i<nums.length-1; i++)\n        {\n            for(int j=i+1; j<nums.length; j++)\n            {\n                if(nums[i]==nums[j] && (i*j)%k==0)\n                    count++;\n            }\n        }\n        return count;\n    }\n}\n\n\n2. Optimised Solution\n\nclass Solution {\n    public int countPairs(int[] nums, int k) {\n        HashMap<Integer, List<Integer>> map= new HashMap();\n        int count=0;\n        for(int i=0; i<nums.length; i++)\n        {\n            if(!map.containsKey(nums[i]))\n            {\n                List<Integer> list= new ArrayList<>();\n                list.add(i);\n                map.put(nums[i], list);\n            }\n            else\n            {\n                List<Integer> list= map.get(nums[i]);\n                for(int j: list)\n                {\n                    if((i * j)%k==0)\n                        count++;\n                }\n                list.add(i);\n                map.put(nums[i], list);\n            }\n        }\n        return count;\n    }\n}
['Java']