📌  相关文章
📜  使两个给定数组的和为偶数所需的相同索引元素的最小交换

📅  最后修改于: 2021-09-03 03:45:02             🧑  作者: Mango

给定两个大小为N 的数组arr1[]arr2[] ,任务是计算数组arr1[]arr2[]中相同索引元素的最小交换次数,以使两者的所有元素相加数组甚至。如果不可能,则打印“-1”

例子:

方法:该想法基于以下观察结果,假设数组arr1[]的总和是sumArr1 ,而arr2[]总和sumArr2

  • 如果 sumArr1 是偶数并且 sumArr2 是偶数:不需要交换。
  • 如果 sumArr1 为奇数且 sumArr2 为奇数:找到一对总和为奇数的相同索引元素并交换它们。这样的一对包含一个偶数和一个奇数。交换它们会使一个数组的总和增加1并将另一个数组的总和减少1 。因此,两个数组的和是偶数。
  • 如果 sumArr1 为偶数且 sumArr2 为奇数:不可能使两个数组的总和为偶数。
  • 如果 sumArr1 是奇数而 sumArr2 是偶数:不可能使两个数组的和为偶数。

请按照以下步骤解决问题:

  • 初始化sumArr1 = 0sumArr2 = 0分别存储arr1[]arr2[] 的和。
  • 如果发现sumArr1sumArr2都是偶数,则打印0
  • 如果发现sumArr1sumArr2都是奇数,则在[0, N – 1]范围内迭代一个循环并检查是否存在任何对应的和为奇数的对。如果找到任何这样的对,则打印1
  • 否则,对于所有其他情况,打印-1

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// 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
void minimumSwaps(int arr1[], int arr2[],
                  int n)
{
    // Store the sum of elements of
    // the array arr1 and arr2 respectively
    int sumArr1 = 0, sumArr2 = 0;
 
    // Store the array sum of both the arrays
    for (int i = 0; i < n; ++i) {
        sumArr1 += arr1[i];
        sumArr2 += arr2[i];
    }
 
    // If both sumArr1 and sumArr2
    // are even, print 0 and return
    if (sumArr1 % 2 == 0
        && sumArr2 % 2 == 0) {
        cout << 0;
        return;
    }
 
    // If both sumArr1 and sumArr2
    // are odd and check for a pair
    // with sum odd sum
    if (sumArr1 % 2 != 0
        && sumArr2 % 2 != 0) {
 
        // Stores if a pair with
        // odd sum exists or not
        int flag = -1;
 
        // Traverse the array
        for (int i = 0; i < n; ++i) {
 
            // If a pair exists with odd
            // sum, set flag = 1
            if ((arr1[i] + arr2[i]) % 2 == 1){
                flag = 1;
                break;
            }
        }
 
        // Print the answer and return
        cout << flag;
 
        return;
    }
 
    // For all other cases, print -1
    cout << -1;
}
 
// Driver Code
int main()
{
    int arr1[] = { 11, 14, 20, 2 };
    int arr2[] = { 5, 9, 6, 3 };
    int N = sizeof(arr1) / sizeof(arr1[0]);
 
    // Function Call
    minimumSwaps(arr1, arr2, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// 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
static void minimumSwaps(int arr1[], int arr2[],
                         int n)
{
     
    // Store the sum of elements of
    // the array arr1 and arr2 respectively
    int sumArr1 = 0, sumArr2 = 0;
 
    // Store the array sum of both the arrays
    for(int i = 0; i < n; ++i)
    {
        sumArr1 += arr1[i];
        sumArr2 += arr2[i];
    }
 
    // If both sumArr1 and sumArr2
    // are even, print 0 and return
    if (sumArr1 % 2 == 0 && sumArr2 % 2 == 0)
    {
        System.out.print(0);
        return;
    }
 
    // If both sumArr1 and sumArr2
    // are odd and check for a pair
    // with sum odd sum
    if (sumArr1 % 2 != 0 && sumArr2 % 2 != 0)
    {
         
        // Stores if a pair with
        // odd sum exists or not
        int flag = -1;
 
        // Traverse the array
        for(int i = 0; i < n; ++i)
        {
             
            // If a pair exists with odd
            // sum, set flag = 1
            if ((arr1[i] + arr2[i]) % 2 == 1)
            {
                flag = 1;
                break;
            }
        }
 
        // Print the answer and return
        System.out.print(flag);
        return;
    }
 
    // For all other cases, print -1
    System.out.print(-1);
}
 
// Driver code
public static void main(String[] args)
{
    int arr1[] = { 11, 14, 20, 2 };
    int arr2[] = { 5, 9, 6, 3 };
    int N = arr1.length;
     
    // Function Call
    minimumSwaps(arr1, arr2, N);
}
}
 
// This code is contributed by jithin


Python3
# Python program for the above approach
 
# 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
def minimumSwaps(arr1, arr2, n):
   
    # Store the sum of elements of
    # the array arr1 and arr2 respectively
    sumArr1 = 0; sumArr2 = 0;
 
    # Store the array sum of both the arrays
    for i in range(n):
        sumArr1 += arr1[i];
        sumArr2 += arr2[i];
 
    # If both sumArr1 and sumArr2
    # are even, pr0 and return
    if (sumArr1 % 2 == 0 and sumArr2 % 2 == 0):
        print(0);
        return;
 
    # If both sumArr1 and sumArr2
    # are odd and check for a pair
    # with sum odd sum
    if (sumArr1 % 2 != 0 and sumArr2 % 2 != 0):
 
        # Stores if a pair with
        # odd sum exists or not
        flag = -1;
 
        # Traverse the array
        for i in range(n):
 
            # If a pair exists with odd
            # sum, set flag = 1
            if ((arr1[i] + arr2[i]) % 2 == 1):
                flag = 1;
                break;
 
        # Print the answer and return
        print(flag);
        return;
 
    # For all other cases, pr-1
    print(-1);
 
# Driver code
if __name__ == '__main__':
    arr1 = [11, 14, 20, 2];
    arr2 = [5, 9, 6, 3];
    N = len(arr1);
 
    # Function Call
    minimumSwaps(arr1, arr2, N);
 
    # This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach 
using System;
class GFG{
  
// 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
static void minimumSwaps(int[] arr1, int[] arr2,
                         int n)
{
      
    // Store the sum of elements of
    // the array arr1 and arr2 respectively
    int sumArr1 = 0, sumArr2 = 0;
  
    // Store the array sum of both the arrays
    for(int i = 0; i < n; ++i)
    {
        sumArr1 += arr1[i];
        sumArr2 += arr2[i];
    }
  
    // If both sumArr1 and sumArr2
    // are even, print 0 and return
    if (sumArr1 % 2 == 0 && sumArr2 % 2 == 0)
    {
        Console.Write(0);
        return;
    }
  
    // If both sumArr1 and sumArr2
    // are odd and check for a pair
    // with sum odd sum
    if (sumArr1 % 2 != 0 && sumArr2 % 2 != 0)
    {
          
        // Stores if a pair with
        // odd sum exists or not
        int flag = -1;
  
        // Traverse the array
        for(int i = 0; i < n; ++i)
        {
              
            // If a pair exists with odd
            // sum, set flag = 1
            if ((arr1[i] + arr2[i]) % 2 == 1)
            {
                flag = 1;
                break;
            }
        }
  
        // Print the answer and return
        Console.Write(flag);
        return;
    }
  
    // For all other cases, print -1
    Console.Write(-1);
}
  
// Driver code
public static void Main()
{
    int[] arr1 = { 11, 14, 20, 2 };
    int[] arr2 = { 5, 9, 6, 3 };
    int N = arr1.Length;
      
    // Function Call
    minimumSwaps(arr1, arr2, N);
}
}
 
// This code is contributed by susmitakundugoaldanga


Javascript


输出:
1

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live