Minimize replacements to make every element in an array exceed every element in another given array | Function to find the minimize replacements to make every element in the array A [ ] strictly greater than every element in B [ ] or vice - versa ; Store the final result ; Create two arrays and initialize with 0 s ; Traverse the array a [ ] ; Increment prefix_a [ a [ i ] ] by 1 ; Traverse the array b [ ] ; Increment prefix_b [ b [ i ] ] by 1 ; Calculate prefix sum of the array a [ ] ; Calculate prefix sum of the array b [ ] ; Iterate over the range [ 0 , 9 ] ; Make every element in array a [ ] strictly greater than digit i and make every element in the array b [ ] less than digit i ; Make every element in array b [ ] strictly greater than digit i and make every element in array a [ ] less than digit i ; Print the answer ; Driver Code
function Mletime ( a , b , n , m ) { let ans = 2147483647 ; let prefix_a = [ ] ; for ( let i = 0 ; i < 10 ; i ++ ) { prefix_a [ i ] = 0 ; } let prefix_b = [ ] ; for ( let i = 0 ; i < 10 ; i ++ ) { prefix_b [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { prefix_a [ a [ i ] ] ++ ; } for ( let i = 0 ; i < m ; i ++ ) { prefix_b [ b [ i ] ] ++ ; } for ( let i = 1 ; i <= 9 ; i ++ ) { prefix_a [ i ] += prefix_a [ i - 1 ] ; } for ( let i = 1 ; i <= 9 ; i ++ ) { prefix_b [ i ] += prefix_b [ i - 1 ] ; } for ( let i = 0 ; i <= 9 ; i ++ ) { ans = Math . min ( ans , prefix_a [ i ] + m - prefix_b [ i ] ) ; ans = Math . min ( ans , n - prefix_a [ i ] + prefix_b [ i ] ) ; } document . write ( ans ) ; } let A = [ 0 , 0 , 1 , 3 , 3 ] ; let B = [ 2 , 0 , 3 ] ; let N = A . length ; let M = B . length ; Mletime ( A , B , N , M ) ;