Skip to content

Commit c62b8b3

Browse files
committed
refactor: use std::accumulate for min/max sum checks
Replace manual loops with std::accumulate to simplify the code and reduce repetition
1 parent 630cfec commit c62b8b3

File tree

1 file changed

+2
-10
lines changed

1 file changed

+2
-10
lines changed

src/CompositionsDistinctUtils.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,14 @@ int GetFirstPartitionDistinct(const std::vector<int> &v, std::vector<int> &z,
7474
int target, int m, int lenV) {
7575

7676
// Compute maximum possible sum: largest m elements
77-
int testMax = 0;
78-
79-
for (int i = lenV - m; i < lenV; ++i) {
80-
testMax += v[i];
81-
}
77+
const int testMax = std::accumulate(v.end() - m, v.end(), 0);
8278

8379
if (testMax < target) {
8480
return -2; // length too small
8581
}
8682

8783
// Compute minimum possible sum: smallest m elements
88-
int testMin = 0;
89-
90-
for (int i = 0; i < m; ++i) {
91-
testMin += v[i];
92-
}
84+
const int testMin = std::accumulate(v.begin(), v.begin() + m, 0);
9385

9486
if (testMin > target) {
9587
return -1; // length too large

0 commit comments

Comments
 (0)