-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequilibrium-index.cpp
More file actions
61 lines (47 loc) · 1.06 KB
/
equilibrium-index.cpp
File metadata and controls
61 lines (47 loc) · 1.06 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
58
59
60
61
/*
Given an array of integers, find the equilibrium index.
An equilibrium index is an index such that the sum of elements
at lower indexes is equal to the sum of
elements at higher indexes.
Example:
Input
7
-7 1 5 2 -4 3 0
Output
3
Explanation: At index 3, the sum of elements before it is -1 and after it is also -1
Sum of before index 3 = -7 + 1 + 5 = -1
Sum of after index 3 = -4 + 3 + 0 = -1
*/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
using namespace std;
void equilibrium_index(vector<int>arr){
int i = 1;
int j = arr.size() - 1;
if((arr[j] - arr[0]) == 0)
cout << i-1 << " ";
while(i <= j){
int previous = arr[i-1];
int next = arr[j] - arr[i];
if(!(previous-next))
cout << i << " ";
i++;
}
}
int main(){
int n;
cin >> n;
vector<int> arr(n);
for(int i = 0; i<n; i++){
cin >> arr[i];
}
for(int i = 1; i<n; i++){
arr[i] += arr[i-1];
}
equilibrium_index(arr);
return 0;
}