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
def MinTime ( a , b , n , m ) : NEW_LINE INDENT ans = float ( ' inf ' ) NEW_LINE prefix_a = [ 0 ] * 10 NEW_LINE prefix_b = [ 0 ] * 10 NEW_LINE for i in range ( n ) : NEW_LINE INDENT prefix_a [ a [ i ] ] += 1 NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT prefix_b [ b [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT prefix_a [ i ] += prefix_a [ i - 1 ] NEW_LINE DEDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT prefix_b [ i ] += prefix_b [ i - 1 ] NEW_LINE DEDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT ans = min ( ans , prefix_a [ i ] + m - prefix_b [ i ] ) NEW_LINE ans = min ( ans , n - prefix_a [ i ] + prefix_b [ i ] ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT A = [ 0 , 0 , 1 , 3 , 3 ] NEW_LINE B = [ 2 , 0 , 3 ] NEW_LINE N = len ( A ) NEW_LINE M = len ( B ) NEW_LINE MinTime ( A , B , N , M ) NEW_LINE