Skip to content

Instantly share code, notes, and snippets.

@VahidSaadat
Created November 30, 2016 20:13
Show Gist options
  • Save VahidSaadat/f1eddcd5c150f196d2208cc1b7920ade to your computer and use it in GitHub Desktop.
Save VahidSaadat/f1eddcd5c150f196d2208cc1b7920ade to your computer and use it in GitHub Desktop.
TapeEquilibrium :)
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