Equilibrium point

PROBLEM :

Given an array A your task is to tell at which position the equilibrium first occurs in the array. Equilibrium position in an array is a position such that the sum of elements below it is equal to the sum of elements after it.

Input:
The first line of input contains an integer T denoting the no of test cases then T test cases follow. First line of each test case contains an integer N denoting the size of the array. Then in the next line are N space separated values of the array A.

Output:
For each test case in a new  line print the position at which the elements are at equilibrium if no equilibrium point exists print -1.

Constraints:
1<=T<=100
1<=N<=100

Example:
Input:
2
1
1
5
1 3 5 2 2

Output:
1
3

Explanation:
1. Since its the only element hence its the only equilibrium point
2. For second test case equilibrium point is at position 3 as elements below it (1+3) = elements after it (2+2)

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int Equilibrium_point(int [],int ) ;
int main()
 {
int t,i,arr[100],no ;
cin>>t ;
while(t--)
{
   cin>>no ;
   for(i=0;i<no;i++)
       cin>>arr[i] ;
     
   cout<<Equilibrium_point(arr,no) ;
   cout<<endl ;
}
return 0;
}

int Equilibrium_point(int arr[],int no)
{
    int i,sum ;
    sum=0 ;
 
    for(i=0;i<no;i++)
        sum=sum+arr[i] ;
 
    int prevSum=0 ;  
    for(i=0;i<no;i++)
    {
        if(prevSum==sum-arr[i]-prevSum)
            return i+1 ;
     
        prevSum=prevSum+arr[i] ;
    }
    return -1 ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )