Equilibrium index of an array | Program to find equilibrium index of an array ; Taking the prefixsum from front end array ; Taking the prefixsum from back end of array ; Checking if forward prefix sum is equal to rev prefix sum ; If You want all the points of equilibrium create vector and push all equilibrium points in it and return the vector ; Driver code
function equilibrium ( a , n ) { if ( n == 1 ) return ( 0 ) ; var forward = new Array ( 0 ) ; var rev = new Array ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( i ) { forward [ i ] = forward [ i - 1 ] + a [ i ] ; } else { forward [ i ] = a [ i ] ; } } for ( let i = n - 1 ; i > 0 ; i -- ) { if ( i <= n - 2 ) { rev [ i ] = rev [ i + 1 ] + a [ i ] ; } else { rev [ i ] = a [ i ] ; } } for ( let i = 0 ; i < n ; i ++ ) { if ( forward [ i ] == rev [ i ] ) { return i ; } } return - 1 ; } arr = new Array ( - 7 , 1 , 5 , 2 , - 4 , 3 , 0 ) ; n = arr . length ; document . write ( " " + equilibrium ( arr , n ) + " " ) ;