The Count Equal and Divisible Pairs in an Array interview question asks you to find the number of pairs of indices (i, j) such that 0 <= i < j < n and two conditions are met:
nums[i] == nums[j](i * j) is divisible by a given integer k.Microsoft and Meta use the Array interview pattern for this problem to test your ability to handle multi-condition checks on arrays. It’s an "Easy" difficulty problem where a simple brute-force approach is usually acceptable given the constraints. It evaluates your basic loop construction and modulo arithmetic skills.
The problem uses a Brute Force / Enumeration pattern.
(i, j) using two nested loops where the inner loop starts from i + 1.nums[i] == nums[j].(i * j) % k == 0.nums = [3, 1, 2, 2, 2, 1, 3], k = 2
(0, 6): nums[0]=3, nums[6]=3. 0 * 6 = 0. 0 % 2 == 0. (Match!)(2, 3): nums[2]=2, nums[3]=2. 2 * 3 = 6. 6 % 2 == 0. (Match!)(2, 4): nums[2]=2, nums[4]=2. 2 * 4 = 8. 8 % 2 == 0. (Match!)(3, 4): nums[3]=2, nums[4]=2. 3 * 4 = 12. 12 % 2 == 0. (Match!)
Result: 4.i + 1, which leads to double-counting pairs.0 % k is always 0. Since indices are 0-based, the first index 0 will always satisfy the product condition.Always check the constraints. If N is small (like 100), O(N^2) is the expected solution. Don't waste time on complex optimizations unless the constraints are much larger (e.g., 10^5).