Find the winner of the game based on greater number of divisors | Function to return the count of divisors of elem ; Function to return the winner of the game ; Convert every element of A [ ] to their divisor count ; Convert every element of B [ ] to their divisor count ; Sort both the arrays ; For every element of A apply Binary Search to find number of pairs where A wins ; B wins if A doesnot win ; Driver code
function divisorcount ( elem ) { let ans = 0 ; for ( let i = 1 ; i <= Math . sqrt ( elem ) ; i ++ ) { if ( elem % i == 0 ) { if ( i * i == elem ) ans ++ ; else ans += 2 ; } } return ans ; } function findwinner ( A , B , N , M ) { for ( let i = 0 ; i < N ; i ++ ) { A [ i ] = divisorcount ( A [ i ] ) ; } for ( let i = 0 ; i < M ; i ++ ) { B [ i ] = divisorcount ( B [ i ] ) ; } 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 ; while ( start <= end ) { let mid = Math . floor ( ( start + end ) / 2 ) ; if ( B [ mid ] <= val ) { index = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } winA += ( index + 1 ) ; } let winB = N * M - winA ; if ( winA > winB ) { return " " ; } else if ( winB > winA ) { return " " ; } return " " ; } let A = [ 4 , 12 , 24 ] ; let N = A . length ; let B = [ 25 , 28 , 13 , 45 ] ; let M = B . length ; document . write ( findwinner ( A , B , N , M ) ) ;