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)wherenis the size ofnums- Space:
O(1)
time taken
- 1 minutes and 16 seconds