HR Find Median ( Priority Queue )
https://www.hackerrank.com/challenges/find-median-1
The classical two heap technique.. Don't forget to add ( int ) before calling size for container..
void solve(){ int N; cin >> N; vi A( N ); for( int i = 0; i < N; ++i ) cin >> A[ i ]; priority_queue< int > small, large; for( int i = 0; i < N; ++i ){ if( small.empty() || A[ i ] <= small.top() ) small.push( A[ i ] ); else large.push( -A[ i ] ); while( ( int )large.size() - ( int )small.size() > 1 ) small.push( -large.top() ), large.pop(); while( ( int )small.size() - ( int )large.size() > 1 ) large.push( -small.top() ), small.pop(); if( small.size() == large.size() ) cout << fixed << setprecision( 1 ) << 1.0 * ( small.top() - large.top() ) / 2; else if( small.size() > large.size() ) cout << fixed << setprecision( 1 ) << 1.0 * small.top(); else cout << fixed << setprecision( 1 ) << 1.0 * -large.top(); cout << endl; } }