8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent de2d6e5 commit 7b9734dCopy full SHA for 7b9734d
practice/problems/3sum_with_multiplicity/solution.java
@@ -0,0 +1,24 @@
1
+class Solution {
2
+ public int threeSumMulti(int[] arr, int target) {
3
+ int mod = 1_000_000_007;
4
+ long result = 0;
5
+ long[] c = new long[101];
6
+ for(int i : arr) c[i]++;
7
+ for(int i = 0; i <= 100; i++) {
8
+ for(int j = i; j <= 100; j++) {
9
+ int k = target - i - j;
10
+ if(k < 0 || k > 100) continue;
11
+ if(i == j && j == k) {
12
+ result += (c[i] * (c[i] - 1) * (c[i] - 2) / 6);
13
+ }
14
+ else if(i == j && j != k) {
15
+ result += ((c[i] * (c[i] - 1) / 2) * c[k]);
16
17
+ else if(i < j && j < k) {
18
+ result += (c[i] * c[j] * c[k]);
19
20
21
22
+ return (int)(result%mod);
23
24
+}
0 commit comments