-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
49 lines (41 loc) · 1.17 KB
/
Copy pathsolution.cpp
File metadata and controls
49 lines (41 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
49
class Solution
{
public:
int cntWays(vector<int> &arr)
{
int n = arr.size();
int totalEven = 0, totalOdd = 0;
// Step 1: calculate total even and odd index sums
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
totalEven += arr[i];
else
totalOdd += arr[i];
}
int leftEven = 0, leftOdd = 0;
int count = 0;
// Step 2: try removing each index
for (int i = 0; i < n; i++)
{
// Remove current element from total
if (i % 2 == 0)
totalEven -= arr[i];
else
totalOdd -= arr[i];
// After removal, right side indices shift
int newEvenSum = leftEven + totalOdd;
int newOddSum = leftOdd + totalEven;
if (newEvenSum == newOddSum)
{
count++;
}
// Add current element to left sums
if (i % 2 == 0)
leftEven += arr[i];
else
leftOdd += arr[i];
}
return count;
}
};