Sort both the arrays
A . sort ( function ( a , b ) { return a - b ; } ) ; B . sort ( function ( a , b ) { return a - b ; } ) ; let winA = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let val = A [ i ] ; let start = 0 ; let end = M - 1 ; let index = - 1 ;