Skip to content

← leetcode

Count Equal and Divisible Pairs in an Array

· Easy

LeetCode problem 2176

Another brute-force solution. Not much else to say other than iterate through all pairs and check if satisfies the conditions present in the question description.

code

class Solution {
public:
    int countPairs(vector<int>& nums, int k) {
        int ans = 0;
        for (int i = 0; i < nums.size(); ++i) {
            for (int j = i+1; j < nums.size(); ++j) {
                if (nums[i] == nums[j] && (i*j)%k == 0) {
                    ++ans;
                }
            }
        }
        return ans;
    }
};

complexity

  • Time: O(n^2) where n is the size of nums
  • Space: O(1)

time taken

  • 1 minutes and 16 seconds