Theft at World Bank | Custom comparator ; Function to find the maximum profit ; Stores the pairs of elements of B and A at the same index ; Iterate over the range [ 0 , N ] ; If current integer is perfect square ; Push the pair of B [ i ] and A [ i ] in vector V ; Sorts the vector using the custom comparator ; Stores the maximum profit ; Traverse the vector V ; If V [ i ] [ 1 ] is less than W ; Increment profit by V [ i ] [ 0 ] ; Decrement V [ i ] [ 1 ] from W ; Otherwise ; Update profit ; Return the value of profit ; Driver Code
function comp ( p1 , p2 ) { let a = p1 [ 0 ] , b = p1 [ 1 ] ; let c = p2 [ 0 ] , d = p2 [ 1 ] ; let val1 = 0 , val2 = 0 ; val1 = Math . floor ( a / b ) ; val2 = Math . floor ( c / d ) ; return val1 > val2 ; } function maximumProfit ( A , B , N , W ) { let V = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { let temp = Math . sqrt ( A [ i ] ) ; if ( temp * temp == A [ i ] ) continue ; V . push ( [ B [ i ] , A [ i ] ] ) ; } V . sort ( comp ) ; let profit = 0.00 ; for ( let i = 0 ; i < V . length ; i ++ ) { if ( V [ i ] [ 1 ] <= W ) { profit += V [ i ] [ 0 ] ; W -= V [ i ] [ 1 ] ; } else { profit += V [ i ] [ 0 ] * ( W / V [ i ] [ 1 ] ) ; break ; } } return profit . toFixed ( 5 ) ; } let N = 3 ; let W = 10 ; let A = [ 4 , 5 , 7 ] ; let B = [ 8 , 5 , 4 ] ; document . write ( maximumProfit ( A , B , N , W ) + " " ) ;