File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ // Time: O(d^3), d is the number of distinct nums
2+ // Space: O(d)
3+
4+ // combinatorics
5+ class Solution {
6+ public:
7+ long long singleDivisorTriplet (vector<int >& nums) {
8+ const auto & check = [](int a, int b, int c) {
9+ const int total = a + b + c;
10+ return (total % a == 0 ) + (total % b == 0 ) + (total % c == 0 ) == 1 ;
11+ };
12+ unordered_map<int , int64_t > cnt;
13+ for (const auto & x : nums) {
14+ ++cnt[x];
15+ }
16+ vector<int > unique_nums;
17+ for (const auto & [k, _] : cnt) {
18+ unique_nums.emplace_back (k);
19+ }
20+ int64_t result = 0 ;
21+ for (int i = 0 ; i < size (unique_nums); ++i) {
22+ for (int j = i + 1 ; j < size (unique_nums); ++j) {
23+ for (int k = j + 1 ; k < size (unique_nums); ++k) {
24+ if (check (unique_nums[i], unique_nums[j], unique_nums[k])) {
25+ result += cnt[unique_nums[i]] * cnt[unique_nums[j]] * cnt[unique_nums[k]];
26+ }
27+ }
28+ }
29+ }
30+ for (int i = 0 ; i < size (unique_nums); ++i) {
31+ for (int j = 0 ; j < size (unique_nums); ++j) {
32+ if (check (unique_nums[i], unique_nums[i], unique_nums[j])) {
33+ result += cnt[unique_nums[i]] * (cnt[unique_nums[i]] - 1 ) / 2 * cnt[unique_nums[j]];
34+ }
35+ }
36+ }
37+ return result * 6 ;
38+ }
39+ };
You can’t perform that action at this time.
0 commit comments