[AlgoExpert] Balance Index

Balance Index

  • Time : O(n)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
int balanceIndex(vector<int> array) {
long long r = accumulate(begin(array), end(array), 0ll), n = array.size(), l = 0;
for(int i = 0; i < n; i++) {
r -= array[i];
if(l == r) return i;
l += array[i];
}
return -1;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/02/PS/AlgoExpert/balance-index/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.