Advertisement
nikunjsoni

923

Apr 17th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int threeSumMulti(vector<int>& A, int target) {
  4.         unordered_map<int, long> c;
  5.         for(int n : A) c[n]++;
  6.         long res = 0;
  7.         for(auto it : c)
  8.             for(auto it2 : c){
  9.                 int i = it.first, j = it2.first, k = target - i - j;
  10.                 if (!c.count(k)) continue;
  11.                 if (i == j && j == k)
  12.                     res += c[i] * (c[i] - 1) * (c[i] - 2) / 6;
  13.                 else if (i == j && j != k)
  14.                     res += c[i] * (c[i] - 1) / 2 * c[k];
  15.                 else if (i < j && j < k)
  16.                     res += c[i] * c[j] * c[k];
  17.             }
  18.         return res % int(1e9 + 7);
  19.     }
  20. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement