# CENTREPOS -Editorial

Author: rocknot
Tester: ramagrawal2001
Editorialist: rocknot

EASY-MEDIUM

Prefix sum

# PROBLEM:

Aditya just completed his aeronautical engineering and got a job at a commercial airline. As he was new he was given a task of managing the load of the airplane. He was given list of relative weights of load and he need to determine the balancing point of this list of loads. You are given an array arr[] of length N

you need to determine the balancing index of the array if it is not possible to find any balancing index return “-1”. balancing index is index at which sum of all the elements of left sub array is equal to sum of all the elements of right sub array.

Note! Indexing starts from 0.

# HINT:

create two arrays and store prefix sum of the given array from start and one from end
and transverse both arrays and find the index at which prefix sum of both arrays is same

# SOLUTIONS:

Solution
#include <bits/stdc++.h>
using namespace std;

int equilibrium(int a[], int n){
if (n == 1)
return (0);
int forward[n] = { 0 };
int rev[n] = { 0 };
for (int i = 0; i < n; i++) {
if (i) {
forward[i] = forward[i - 1] + a[i];
}
else {
forward[i] = a[i];
}
}
for (int i = n - 1; i > 0; i--) {
if (i <= n - 2) {
rev[i] = rev[i + 1] + a[i];
}
else {
rev[i] = a[i];
}
}
for (int i = 0; i < n; i++) {
if (forward[i] == rev[i]) {
return i;
}
}
return -1;

}
int main(){
int T;
cin>>T;
while(T–){
int N;
cin>>N;
int arr[N];
for(int i=0;i<N;i++){
cin>>arr[i];
}
cout<< equilibrium(arr, N) << “\n”;
}
return 0;
}