a few solutions
Use a map m to create "buckets" index for each same value x, then consider each i,j index pair as a candidate for which we increment the count cnt per the probl
Use a map `m` to create "buckets" index for each same value `x`, then consider each `i`,`j` index pair as a candidate for which we increment the count `cnt` per the problem statement.\n\n---\n\n*Kotlin*\n```\nclass Solution {\n    fun countPairs(A: IntArray, K: Int): Int {\n        var m = mutableMapOf<Int, MutableList<Int>>()\n        for ((i, x) in A.withIndex()) {\n            if (!m.contains(x))\n                m[x] = mutableListOf<Int>()\n            m[x]!!.add(i)\n        }\n        var cnt = 0\n        for ((_, index) in m)\n            for (i in index)\n                for (j in index)\n                    cnt += if (i < j && (i * j) % K == 0) 1 else 0\n        return cnt\n    }\n}\n```\n\n*Javascript*\n```\nlet countPairs = (A, K, m = new Map(), cnt = 0) => {\n    for (let i = 0; i < A.length; ++i) {\n        if (!m.has(A[i]))\n            m.set(A[i], []);\n        m.get(A[i]).push(i);\n    }\n    for (let [_, index] of m)\n        for (let i of index)\n            for (let j of index)\n                cnt += Number(i < j && (i * j) % K == 0);\n    return cnt;\n};\n```\n\n*Python3*\n```\nclass Solution:\n    def countPairs(self, A: List[int], K: int, cnt = 0) -> int:\n        m = defaultdict(list)\n        for i, x in enumerate(A):\n            m[x].append(i)\n        for index in m.values():\n            for i in index:\n                for j in index:\n                    cnt += int(i < j and (i * j) % K == 0)\n        return cnt\n```\n\n*Rust*\n```\nuse std::collections::HashMap;\ntype VI = Vec<i32>;\nimpl Solution {\n    pub fn count_pairs(A: VI, K: i32) -> i32 {\n        let mut cnt = 0;\n        let mut m = HashMap::new();\n        for (i, x) in A.iter().enumerate() {\n            let mut entry = m.entry(x).or_insert(vec![]);\n            entry.push(i);\n        }\n        for (x, index) in &m {\n            for i in index {\n                for j in index {\n                    if i < j && (i * j) % K as usize == 0 { cnt += 1 }\n                }\n            }\n        }\n        return cnt;\n    }\n}\n```\n\n*C++*\n```\nclass Solution {\npublic:\n    using VI = vector<int>;\n    using Map = unordered_map<int, VI>;\n    int countPairs(VI& A, int K, Map m = {}, int cnt = 0) {\n        for (auto i{ 0 }; i < A.size(); ++i)\n            m[A[i]].push_back(i);\n        for (auto [x, index]: m)\n            for (auto i: index)\n                for (auto j: index)\n                    cnt += i < j && (i * j) % K == 0;\n        return cnt;\n    }\n};\n```
[]