Skip to content

Instantly share code, notes, and snippets.

@SasiZets
Created March 7, 2020 06:01
Show Gist options
  • Save SasiZets/ce61227fc5a0dc5cb9188e764b0345b1 to your computer and use it in GitHub Desktop.
Save SasiZets/ce61227fc5a0dc5cb9188e764b0345b1 to your computer and use it in GitHub Desktop.
Given an array A of N positive numbers. The task is to find the position where equilibrium first occurs in the array. Equilibrium position in an array is a position such that the sum of elements before it is equal to the sum of elements after it. Input: The first line of input contains an integer T, denoting the number of test cases. Then T test…
void main() {
List a=[1,4,5,2,2];
int sum=0;
int leftSum=0;
for(int v in a)
sum+=v;
for(int i=0;i<a.length;i++){
sum-=a[i];
if(sum==leftSum){
print(a[i]);
return;
}
leftSum+=a[i];
}
print('-1');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment