-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubarrayDivisibility.java
More file actions
29 lines (27 loc) · 924 Bytes
/
SubarrayDivisibility.java
File metadata and controls
29 lines (27 loc) · 924 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
import java.io.*;
import java.util.*;
class SubarrayDivisibility {
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String str[] = br.readLine().split(" ");
long ans = 0l;
HashMap<Long, Long> map = new HashMap<>();
long sum = 0l;
map.put(0l, 1l);
for(String s : str) {
sum += Long.parseLong(s);
long rem = sum % n;
if(rem >= 0) {
ans += map.getOrDefault(rem, 0l);
map.put(rem, map.getOrDefault(rem, 0l) + 1l);
} else {
rem = n + rem;
ans += map.getOrDefault(rem, 0l);
map.put(rem, map.getOrDefault(rem, 0l) + 1l);
}
}
System.out.println(ans);
}
}