📌  相关文章
📜  使数组元素交替为偶数和奇数所需的最小增量

📅  最后修改于: 2021-04-17 17:06:44             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是找到使数组arr []交替由偶数和奇数组成的序列所需的最小增量数。

例子:

方法:为了解决给定的问题,想法是检查使数组元素为奇数和偶数以及奇数和偶数交替所需的增量数。最后,打印获得的两个增量计数中的最小值。请按照以下步骤解决问题:

  • 初始化一个变量,例如cnt0 ,以存储将数组转换为偶数和奇数的交替序列(反之亦然)所需的增量计数。
  • 使用变量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)