给定一个由N 个正整数组成的数组arr[] ,任务是找到使数组arr[]交替排列偶数和奇数所需的最小增量数。
例子:
Input: arr[] = {1, 4, 6, 8, 9, 5}
Output: 2
Explanation:
Incrementing arr[2] modifies arr[] to {1, 4, 7, 8, 9, 5}.
Incrementing arr[5] by 1 modifies arr[] to {1, 4, 7, 8, 9, 6}.
Input: arr[] = {3, 5, 7, 9, 4, 2}
Output: 3
Explanation:
Incrementing arr[0] by 1 modifies arr[] to {4, 5, 7, 9, 4, 2}.
Incrementing arr[2] by 1 modifies arr[] to {4, 5, 8, 9, 4, 2}.
Incrementing arr[5] by 1 modifies arr[] to {4, 5, 8, 9, 4, 3}.
方法:解决给定问题的思路是检查使数组元素奇偶交替以及偶奇交替所需的增量数。最后,打印获得的两个增量计数中的最小值。请按照以下步骤解决问题:
- 初始化一个变量,比如cnt为0 ,以存储将数组转换为偶数和奇数的交替序列所需的增量计数,反之亦然。
- 使用变量i遍历数组arr[]并执行以下步骤:
- 如果i是偶数且arr[i]是奇数,则将cnt增加 1。
- 如果i是奇数而arr[i]是偶数,则将cnt加 1。
- 完成上述步骤后, cnt和(N-cnt)的最小值就是需要的结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number of
// increments required to make the array
// even-odd alternately or vice-versa
int minIncr(int *arr,int n)
{
// Store the minimum number of
// increments required
int forEven = 0;
// Traverse the array arr[]
for(int i = 0; i < n; i++)
{
// Increment forEven if even
// element is present at an
// odd index
if(i % 2){
if((arr[i] % 2) == 0)
forEven += 1;
}
// Increment forEven if odd
// element is present at an
// even index
else{
if(arr[i] % 2)
forEven += 1;
}
}
// Return the minimum number of increments
return min(forEven, n - forEven);
}
int main()
{
// Driver Code
int arr[] = {1, 4, 6, 8, 9, 5};
int n = sizeof(arr)/sizeof(arr[0]);
cout<
Java
// Java program for the above approach
class GFG{
// Function to find the minimum number of
// increments required to make the array
// even-odd alternately or vice-versa
static int minIncr(int []arr, int n)
{
// Store the minimum number of
// increments required
int forEven = 0;
// Traverse the array arr[]
for(int i = 0; i < n; i++)
{
// Increment forEven if even
// element is present at an
// odd index
if (i % 2 == 1)
{
if ((arr[i] % 2) == 0)
forEven += 1;
}
// Increment forEven if odd
// element is present at an
// even index
else
{
if (arr[i] % 2 == 1)
forEven += 1;
}
}
// Return the minimum number of increments
return Math.min(forEven, n - forEven);
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 1, 4, 6, 8, 9, 5 };
int n = arr.length;
System.out.println(minIncr(arr,n));
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to find the minimum number of
# increments required to make the array
# even-odd alternately or vice-versa
def minIncr(arr):
# Store the minimum number of
# increments required
forEven = 0
# Traverse the array arr[]
for i in range(len(arr)):
# Increment forEven if even
# element is present at an
# odd index
if i % 2:
if not arr[i] % 2:
forEven += 1
# Increment forEven if odd
# element is present at an
# even index
else:
if arr[i] % 2:
forEven += 1
# Return the minimum number of increments
return min(forEven, len(arr)-forEven)
# Driver Code
arr = [1, 4, 6, 8, 9, 5]
print(minIncr(arr))
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the minimum number of
// increments required to make the array
// even-odd alternately or vice-versa
static int minIncr(int []arr, int n)
{
// Store the minimum number of
// increments required
int forEven = 0;
// Traverse the array []arr
for(int i = 0; i < n; i++)
{
// Increment forEven if even
// element is present at an
// odd index
if (i % 2 == 1)
{
if ((arr[i] % 2) == 0)
forEven += 1;
}
// Increment forEven if odd
// element is present at an
// even index
else
{
if (arr[i] % 2 == 1)
forEven += 1;
}
}
// Return the minimum number of increments
return Math.Min(forEven, n - forEven);
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 1, 4, 6, 8, 9, 5 };
int n = arr.Length;
Console.WriteLine(minIncr(arr,n));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live