-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquilibriumIndex.cpp
More file actions
39 lines (33 loc) · 821 Bytes
/
EquilibriumIndex.cpp
File metadata and controls
39 lines (33 loc) · 821 Bytes
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
/*
Equilibrium index of an array
Difficulty Level : Easy
Last Updated : 13 Jul, 2022
Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an array A:
Example :
Input: A[] = {-7, 1, 5, 2, -4, 3, 0}
Output: 3
3 is an equilibrium index, because:
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]
Input: A[] = {1, 2, 3}
Output: -1
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int>arr = { 1, 2, 3};
int rsum = 0, lsum = 0;
bool flag = true;
for (int i = 0; i < arr.size(); i++) rsum += arr[i];
for (int j = 0; j < arr.size(); j++) {
rsum -= arr[j];
if (lsum == rsum) {
flag = false;
cout << j << endl;
break;
}
lsum += arr[j];
}
if (flag) cout << -1 << endl;
return 0;
}