📌  相关文章
📜  使数组元素的奇偶校验与它们的索引相同所需的最小交换次数

📅  最后修改于: 2021-04-17 16:22:03             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是找到使所有数组元素的奇偶校验(即,偶数奇数)与它们各自的索引相同所需的最小交换次数。如果无法这样做,请打印“ -1”

例子:

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

  • 0初始化偶数奇数变量,以存储偶数和奇数的计数。
  • 使用变量i遍历数组arr []并执行以下操作:
    • 如果arr [i]i的奇偶性相同,则继续。
    • 检查是否均匀。如果发现是真的, 然后通过递增1的值更新偶数
    • 否则,通过将值增加1来更新奇数
  • 完成上述步骤后,如果偶数奇数的值不相等,则打印-1
  • 否则,将even的值打印为所需的最小交换次数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the minimum number
// of swaps required to make the parity
// of array elements same as their indices
void minimumSwaps(int arr[], int N)
{
    // Stores count of even
    // and odd array elements
    int even = 0, odd = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Check if indices and
        // array elements are not
        // of the same parity
        if (arr[i] % 2 != i % 2) {
 
            // If index is even
            if (i % 2 == 0) {
 
                // Update even
                even++;
            }
            else {
 
                // Update odd
                odd++;
            }
        }
    }
 
    // Condition for not possible
    if (even != odd) {
 
        cout << -1;
    }
 
    // Otherwise
    else {
        cout << even;
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 2, 7, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    minimumSwaps(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to count the minimum number
  // of swaps required to make the parity
  // of array elements same as their indices
  static void minimumSwaps(int arr[], int N)
  {
     
    // Stores count of even
    // and odd array elements
    int even = 0, odd = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
      // Check if indices and
      // array elements are not
      // of the same parity
      if (arr[i] % 2 != i % 2) {
 
        // If index is even
        if (i % 2 == 0) {
 
          // Update even
          even++;
        }
        else {
 
          // Update odd
          odd++;
        }
      }
    }
 
    // Condition for not possible
    if (even != odd) {
 
      System.out.println(-1);
    }
 
    // Otherwise
    else {
      System.out.println(even);
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    int arr[] = { 3, 2, 7, 6 };
    int N = arr.length;
 
    minimumSwaps(arr, N);
  }
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to count the minimum number
# of swaps required to make the parity
# of array elements same as their indices
def minimumSwaps(arr, N):
   
    # Stores count of even
    # and odd array elements
    even, odd = 0, 0
 
    # Traverse the array
    for i in range(N):
 
        # Check if indices and
        # array elements are not
        # of the same parity
        if (arr[i] % 2 != i % 2):
 
            # If index is even
            if (i % 2 == 0):
 
                # Update even
                even += 1
            else:
                # Update odd
                odd += 1
 
    # Condition for not possible
    if (even != odd):
        print(-1)
 
    # Otherwise
    else:
        print(even)
 
# Driver Code
if __name__ == '__main__':
    arr = [3, 2, 7, 6]
    N = len(arr)
 
    minimumSwaps(arr, N)
     
# This code is contributed by mohit kumar 29.


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
  // Function to count the minimum number
  // of swaps required to make the parity
  // of array elements same as their indices
  static void minimumSwaps(int[] arr, int N)
  {
 
    // Stores count of even
    // and odd array elements
    int even = 0, odd = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
      // Check if indices and
      // array elements are not
      // of the same parity
      if (arr[i] % 2 != i % 2) {
 
        // If index is even
        if (i % 2 == 0) {
 
          // Update even
          even++;
        }
        else {
 
          // Update odd
          odd++;
        }
      }
    }
 
    // Condition for not possible
    if (even != odd) {
 
      Console.WriteLine(-1);
    }
 
    // Otherwise
    else {
      Console.WriteLine(even);
    }
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 3, 2, 7, 6 };
    int N = arr.Length;
 
    minimumSwaps(arr, N);
  }
}
 
// This code is contributed by souravghosh0416.


输出:
2

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