Created
November 30, 2016 20:13
-
-
Save VahidSaadat/f1eddcd5c150f196d2208cc1b7920ade to your computer and use it in GitHub Desktop.
TapeEquilibrium :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TapeEquilibrium { | |
public: | |
int tape_equilibrium( int A[] , int N ){ | |
int sum_1 = 0 , sum_2 = 0 , min_sum; | |
for ( int i=0 ; i<N ; i++ ) | |
sum_2 += A[i]; | |
//set minimum sum equal first cell for compair with others | |
min_sum = abs(sum_2 - (2*A[0])); | |
for( int p=1 ; p <= N ; p++ ) { | |
sum_2 -= A[p-1]; | |
sum_1 += A[p-1]; | |
if ( abs( sum_2 - sum_1 ) < min_sum ) | |
min_sum = abs( sum_2 - sum_1 ); | |
} | |
return min_sum; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment