-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
40 lines (35 loc) · 1019 Bytes
/
Solution.java
File metadata and controls
40 lines (35 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.Scanner;
public class Solution {
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int n = scan.nextInt();
int k = scan.nextInt();
int[] ar = new int[n];
for (int i = 0; i < ar.length; i++) {
ar[i] = scan.nextInt();
}
scan.close();
System.out.println(divisibleSumPairs(n, k, ar));
}
static int divisibleSumPairs(int n, int k, int[] ar) {
// O(n) solution
int[] bucket = new int[k];
int count = 0;
for (int num : ar) {
int mod = num % k;
count += bucket[(k - mod) % k];
bucket[mod]++;
}
return count;
/*
O(n^2) solution
int count = 0;
for (int i = 0; i < ar.length; i++) {
for (int j = i + 1; j < ar.length; j++) {
if ((ar[i] + ar[j]) % k == 0) count++;
}
}
return count;
*/
}
}