-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion 75.java
More file actions
53 lines (36 loc) · 876 Bytes
/
Question 75.java
File metadata and controls
53 lines (36 loc) · 876 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
41
42
43
44
45
46
47
48
49
50
51
//Question 75
package hello;
import java.util.*;
class A{
static int subCount(int arr[], int n, int k)
{
int mod[] = new int[k];
Arrays.fill(mod, 0);
int cumSum = 0;
for (int i = 0; i < n; i++) {
cumSum += arr[i];
// as the sum can be negative, taking modulo twice
mod[((cumSum % k) + k) % k]++;
}
// Initialize result
int result = 0;
// Traverse mod[]
for (int i = 0; i < k; i++)
// If there are more than one prefix subarrays
// with a particular mod value.
if (mod[i] > 1)
result += (mod[i] * (mod[i] - 1)) / 2;
// add the elements which are divisible by k itself
// i.e., the elements whose sum = 0
result += mod[0];
return result;
}
//MAIN
public static void main(String[] args)
{
int arr[] = {9,3,1,2,6,3};
int k = 3;
int n = arr.length;
System.out.println(subCount(arr, n, k));
}
}