-
Notifications
You must be signed in to change notification settings - Fork 1
/
Equilibrium Point.cpp
61 lines (46 loc) · 1.02 KB
/
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
57
58
59
60
61
#include <iostream>
using namespace std;
// } Driver Code Ends
class Solution{
public:
// Function to find equilibrium point in the array.
// a: input array
// n: size of array
int equilibriumPoint(long long a[], int n) {
// Your code here
if(n==1)
return 1;
int sum =0;
for(int i =0; i <n; i++)
sum += a[i];
int l_sum =0;
for(int i =0; i < n; i++)
{
sum -= a[i];
if(sum == l_sum)
return i+1;
l_sum += a[i];
}
return -1;
}
};
//{ Driver Code Starts.
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];
}
Solution ob;
//calling equilibriumPoint() function
cout << ob.equilibriumPoint(a, n) << endl;
}
return 0;
}