-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSubarray Sums Divisible by K.cpp
53 lines (36 loc) · 1.17 KB
/
Subarray Sums Divisible by K.cpp
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
52
53
/*
Solution by Rahul Surana
***********************************************************
Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k.
A subarray is a contiguous part of an array.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
long long int ans = 0;
int df(int x){
if(x <= 1) return x;
return x+df(x-1);
}
int subarraysDivByK(vector<int>& nums, int k) {
if(nums.size()==0) return 0;
vector<int> ps(nums.size()+1,0);
// cout << ((-104)%k)<<"\n";
ps[0] = (nums[0]);
for(int i = 1 ;i < nums.size(); i++){
ps[i] = (ps[i-1] +nums[i]) ;
}
vector<int> f(100000,0);
for(int i = 0; i < nums.size(); i++){
f[((ps[i]%k)+k)%k]++;
// cout << i << " "<< ps[i] <<" \n";
}
for(int i = 0; i < k; i++){
if(f[i]>0)
ans += df(f[i]-1);
// cout << f[i]<<"\n";
}
return ans+f[0];
}
};