-
Notifications
You must be signed in to change notification settings - Fork 8
/
[unoriginal]leetcode2845_count_of_interesting_subarrays.cpp
92 lines (73 loc) · 2.13 KB
/
[unoriginal]leetcode2845_count_of_interesting_subarrays.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @file leetcode2845_count_of_interesting_subarrays.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/09/08 17:38
* @brief https://leetcode.com/problems/count-of-interesting-subarrays/
*
**/
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Solution {
public:
/*
* Solution:
* https://leetcode.com/problems/count-of-interesting-subarrays/solutions/3995124/c-count-subarrays-with-sum-mod-k-easy-explanation/
*/
long long countInterestingSubarrays(vector<int>& nums, int modulo, int k) {
int len = nums.size();
std::vector<bool> flag(len, false);
std::map<int, uint64_t> mp;
int n = 0;
uint64_t total = 0;
for (int i = 0; i < len; ++i) {
if (nums[i] % modulo == k) {
flag[i] = true;
++n;
}
int mod = n % modulo;
if (mod == k) {
total++;
}
int find = mod - k;
if (find < 0) {
find += modulo;
}
total += mp[find];
mp[mod]++;
}
return total;
}
};
int main() {
while (1) {
int n = 0;
std::vector<int> nums;
std::cout << "Number of elements (Ctrl-C to exit): " << std::endl;
if (!(std::cin >> n)) {
return 0;
}
std::cout << n << " integers: " << std::endl;
nums.clear();
int ele;
for (int i = 0; i < n; ++i) {
std::cin >> ele;
nums.push_back(ele);
}
int modulo = 0;
std::cout << "Input modulo (Ctrl-C to exit: " << std::endl;
if (!(std::cin >> modulo)) {
return 0;
}
int k = 0;
std::cout << "Input k (Ctrl-C to exit): " << std::endl;
if (!(std::cin >> k)) {
return 0;
}
Solution solution;
auto ret = solution.countInterestingSubarrays(nums, modulo, k);
std::cout << ret << std::endl;
}
return 0;
}