给定一个由N 个整数组成的数组arr[] ,任务是找到使所有数组元素的奇偶校验(即偶数或奇数)与其各自索引相同所需的最小交换次数。如果无法这样做,则打印“-1” 。
例子:
Input: arr[] = {3, 2, 7, 6}
Output: 2
Explanation:
Swap {arr[0], arr[1]} and {arr[2], arr[3]}. The array arr[] modifies to {2, 3, 6, 7}.
Now every odd and even element are at odd and even indices respectively.
Therefore, the minimum number of swaps required is 2.
Input: arr[] = {7}
Output: -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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live