-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_partitions.cpp
More file actions
52 lines (47 loc) · 1.25 KB
/
count_partitions.cpp
File metadata and controls
52 lines (47 loc) · 1.25 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
49
50
51
52
/*
* =====================================================================================
*
* Filename: count_partitions.cpp
*
* Description: 3432. Count Partitions with Even Sum Difference
* https://leetcode.com/problems/count-partitions-with-even-sum-difference/
*
* Version: 1.0
* Created: 01/26/2025 23:42:32
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::pair;
using std::vector;
class Solution {
public:
int countPartitions(vector<int>& nums) {
int odd = 0;
for (const auto& val : nums) {
odd ^= val & 0x1;
}
if (odd || nums.size() == 0) {
return 0;
}
return nums.size() - 1;
}
};
TEST(Solution, countPartitions) {
vector<pair<vector<int>, int>> cases = {
std::make_pair(vector<int>{10, 10, 3, 7, 6}, 4),
std::make_pair(vector<int>{1, 2, 2}, 0),
std::make_pair(vector<int>{2, 4, 6, 8}, 3),
};
for (auto& c : cases) {
EXPECT_EQ(Solution().countPartitions(c.first), c.second);
}
}