Sorting the Both Ap and Bp
Collections . sort ( Ap , ( x , y ) -> { if ( x [ 0 ] != y [ 0 ] ) return x [ 0 ] - y [ 0 ] ; return y [ 1 ] - y [ 1 ] ; } ) ; Collections . sort ( Bp , ( x , y ) -> { if ( x [ 0 ] != y [ 0 ] ) return x [ 0 ] - y [ 0 ] ; return y [ 1 ] - y [ 1 ] ; } ) ; int i = 0 , j = 0 ; int ans [ ] = new int [ n ] ;