Minimize replacements to make every element in an array exceed every element in another given array | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MinTime ( int * a , int * b , int n , int m ) { int ans = INT_MAX ; int prefix_a [ 10 ] = { 0 } ; int prefix_b [ 10 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { prefix_a [ a [ i ] ] ++ ; } for ( int i = 0 ; i < m ; i ++ ) { prefix_b [ b [ i ] ] ++ ; } for ( int i = 1 ; i <= 9 ; i ++ ) { prefix_a [ i ] += prefix_a [ i - 1 ] ; } for ( int i = 1 ; i <= 9 ; i ++ ) { prefix_b [ i ] += prefix_b [ i - 1 ] ; } for ( int i = 0 ; i <= 9 ; i ++ ) { ans = min ( ans , prefix_a [ i ] + m - prefix_b [ i ] ) ; ans = min ( ans , n - prefix_a [ i ] + prefix_b [ i ] ) ; } cout << ans ; } int main ( ) { int A [ ] = { 0 , 0 , 1 , 3 , 3 } ; int B [ ] = { 2 , 0 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int M = sizeof ( B ) / sizeof ( B [ 0 ] ) ; MinTime ( A , B , N , M ) ; return 0 ; }