Minimum swaps of same | Function to count the minimum swaps of same - indexed elements from arrays arr1 and arr2 required to make the sum of both the arrays even ; Store the sum of elements of the array arr1 and arr2 respectively ; Store the array sum of both the arrays ; If both sumArr1 and sumArr2 are even , pr0 and return ; If both sumArr1 and sumArr2 are odd and check for a pair with sum odd sum ; Stores if a pair with odd sum exists or not ; Traverse the array ; If a pair exists with odd sum , set flag = 1 ; Print the answer and return ; For all other cases , pr - 1 ; Driver code ; Function Call
def minimumSwaps ( arr1 , arr2 , n ) : NEW_LINE INDENT sumArr1 = 0 ; sumArr2 = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sumArr1 += arr1 [ i ] ; NEW_LINE sumArr2 += arr2 [ i ] ; NEW_LINE DEDENT if ( sumArr1 % 2 == 0 and sumArr2 % 2 == 0 ) : NEW_LINE INDENT print ( 0 ) ; NEW_LINE return ; NEW_LINE DEDENT if ( sumArr1 % 2 != 0 and sumArr2 % 2 != 0 ) : NEW_LINE INDENT flag = - 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( arr1 [ i ] + arr2 [ i ] ) % 2 == 1 ) : NEW_LINE INDENT flag = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT print ( flag ) ; NEW_LINE return ; NEW_LINE DEDENT print ( - 1 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 11 , 14 , 20 , 2 ] ; NEW_LINE arr2 = [ 5 , 9 , 6 , 3 ] ; NEW_LINE N = len ( arr1 ) ; NEW_LINE minimumSwaps ( arr1 , arr2 , N ) ; NEW_LINE DEDENT