-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequilibrium-point.cpp
73 lines (57 loc) · 1.32 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
62
63
64
65
66
67
68
69
70
71
72
73
//{ Driver Code Starts
#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/
int l=0;
int r=0;
int flag=0;
if(n==1){
return 1;
}
for(int i=1;i<n;i++){
r+=a[i];
}
for(int i=1;i<n;i++){
l += a[i-1];
r -= a[i];
if(l==r){
return i+1;
flag++;
}
else{
continue;
}
}
if(flag == 0){
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;
}
// } Driver Code Ends