-
Notifications
You must be signed in to change notification settings - Fork 0
/
EquilibriumIndex.cpp
45 lines (37 loc) · 1.16 KB
/
EquilibriumIndex.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
//////////////////////////////////////CODE STUDIO////////////////////////////////////
// EASY
//////////////////////////////////////__724__LEETCODE////////////////////////////////////
// EASY
// Q. FInd the Equilibrium index for the array
/* BEST / OPTIMAL :
equilibrium index is the index for which rhs sum =lhs sum
Approach: Here we'll first calculate the sum of the array
then iniliaze lhs sum =0;
In the second loop, we'll decrement the sum i.e. it will act as the rhs summ
then we'll compare lhs rhs , if they are same then return index i
then increment the lsum
otherwise return -1 --> no equilibrium index
*/
// #include <bits/stdc++.h>
// int findEquilibriumIndex(vector<int> &arr)
// {
// // Write your code here.
// int n = arr.size();
// int lsum, sum;
// lsum = 0, sum = 0;
// int i;
// for (i = 0; i < n; i++)
// {
// sum = sum + arr[i];
// }
// for (i = 0; i < n; i++)
// {
// sum = sum - arr[i];
// if (lsum == sum)
// { // here sum is acting as righthandside sum
// return i;
// }
// lsum = lsum + arr[i];
// }
// return -1;
// }