-
Notifications
You must be signed in to change notification settings - Fork 0
/
Equilibrium_Point.cpp
56 lines (40 loc) · 1007 Bytes
/
Equilibrium_Point.cpp
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
#include <iostream>
using namespace std;
// Position this line where user code will be pasted.
int equilibriumPoint(long long a[], int n);
int main() {
long long t;
//taking testcases
cin >> t;
while (t--) {
long long n;
//taking input n
cin >> n;
long long a[n];
//adding elements to the array
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
//calling equilibriumPoint() function
cout << equilibriumPoint(a, n) << endl;
}
return 0;
}
// } Driver Code Ends
// Function to find equilibrium point
// a: input array
// n: size of array
int equilibriumPoint(long long arr[], int n) {
long long int sum = 0;
long long int leftsum = 0;
for (int i = 0; i < n; ++i)
sum += arr[i];
for (int i = 0; i < n; ++i)
{
sum -= arr[i];
if (leftsum == sum)
return i+1;
leftsum += arr[i];
}
return -1;
}