-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_subarrays.cpp
More file actions
48 lines (43 loc) · 1.17 KB
/
count_subarrays.cpp
File metadata and controls
48 lines (43 loc) · 1.17 KB
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
/*
* =====================================================================================
*
* Filename: count_subarrays.cpp
*
* Description: 3392. Count Subarrays of Length Three With a Condition
* https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/
*
* Version: 1.0
* Created: 04/29/2025 22:52:55
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <utility>
#include <vector>
#include "gtest/gtest.h"
using std::vector;
class Solution {
public:
int countSubarrays(vector<int>& nums) {
int count = 0;
for (int i = 1; i < nums.size() - 1; i++) {
if ((nums[i - 1] + nums[i + 1]) * 2 == nums[i]) {
count++;
}
}
return count;
}
};
TEST(Solution, countSubarrays) {
vector<std::pair<vector<int>, int>> cases = {
std::make_pair(vector<int>{1, 2, 1, 4, 1}, 1),
std::make_pair(vector<int>{1, 1, 1}, 0),
};
for (auto& c : cases) {
EXPECT_EQ(Solution().countSubarrays(c.first), c.second);
}
}