Minimize swaps between two arrays such that sum of the first array exceeds sum of the second array | Function to find the minimum count of swaps required between the two arrays to make the sum of arr1 [ ] greater than that of arr2 [ ] ; Stores the sum of the two arrays ; Calculate sum of arr1 [ ] ; Calculate sum of arr2 [ ] ; Sort the arrays arr1 [ ] and arr2 [ ] ; Traverse the array arr [ ] ; If the sum1 is less than or equal to sum2 ; Swapping the elements ; Update the sum1 and sum2 ; Increment the count ; Return the final count ; Driver Code ; Function Call
def maximumCount ( arr1 , arr2 , s1 , s2 ) : NEW_LINE INDENT sum1 = 0 NEW_LINE sum2 = 0 NEW_LINE for i in range ( s1 ) : NEW_LINE INDENT sum1 += arr1 [ i ] NEW_LINE DEDENT for j in range ( s2 ) : NEW_LINE INDENT sum2 += arr2 [ j ] NEW_LINE DEDENT len = 0 NEW_LINE if ( s1 >= s2 ) : NEW_LINE INDENT lenn = s2 NEW_LINE DEDENT else : NEW_LINE INDENT lenn = s1 NEW_LINE DEDENT arr1 . sort ( ) ; NEW_LINE arr2 . sort ( ) ; NEW_LINE j = 0 NEW_LINE k = s2 - 1 NEW_LINE count = 0 NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( sum1 <= sum2 ) : NEW_LINE INDENT if ( arr2 [ k ] >= arr1 [ i ] ) : NEW_LINE INDENT dif1 = arr1 [ j ] NEW_LINE dif2 = arr2 [ k ] NEW_LINE sum1 -= dif1 NEW_LINE sum1 += dif2 NEW_LINE sum2 -= dif2 NEW_LINE sum2 += dif1 NEW_LINE j += 1 NEW_LINE k -= 1 NEW_LINE count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr1 = [ 1 , 3 , 2 , 4 ] NEW_LINE arr2 = [ 6 , 7 , 8 ] NEW_LINE N = len ( arr1 ) NEW_LINE M = len ( arr2 ) NEW_LINE print ( maximumCount ( arr1 , arr2 , N , M ) ) NEW_LINE