-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequilibriumPoint.cpp
More file actions
57 lines (56 loc) · 1.11 KB
/
Copy pathequilibriumPoint.cpp
File metadata and controls
57 lines (56 loc) · 1.11 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
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
#define ll long int
int main()
{
int tc;
cin >> tc;
while (tc--)
{
ll n;
cin >> n;
vector<ll> v;
bool flag = false;
for (auto i = 0; i < n; i++)
{
ll x;
cin >> x;
v.push_back(x);
}
ll i = 0, j = n - 1;
ll fsum = v[0], bsum = v[n - 1];
while (i < j)
{
if (fsum < bsum)
{
i++;
fsum += v[i];
}
else if (fsum > bsum)
{
j--;
bsum += v[j];
}
else if (fsum == bsum)
{
if (i == j - 1)
flag = true;
i++;
j--;
if (i < j)
{
fsum += v[i];
bsum += v[j];
}
}
}
if (fsum == bsum && flag == false)
{
cout << i + 1 << "\n";
}
else
{
cout << -1 << "\n";
}
}
}