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

📅  最后修改于: 2021-10-27 03:22:34             🧑  作者: Mango

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

例子:

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

  • 0初始化变量even奇数,以存储偶数和奇数的计数。
  • 使用变量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.


Javascript


输出:
2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程