First post!

OK, let’s see if this thing works at all… Sample PHP code follows – O(n) solution of equilibrium index problem (enable JavaScript from domains bartlomiejb.wordpress.com and wp.com [with subdomains] to see source code properly highlighted):

function solution( $A ) {
    $cnt = count( $A );
    if( $cnt == 0 )
        return -1;
    if( $cnt == 1 )
        return 0;
    
    $rsum = array_sum( $A );
    $lsum = 0;
    foreach( $A as $k => $v ) {
        $rsum -= $v;
        if( $lsum == $rsum )
            return $k;
        $lsum += $v;
    }
    return -1;
}

Documentation of this feature: http://en.support.wordpress.com/code/posting-source-code/

Advertisements

Leave a Reply / Skomentuj

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s