📌  相关文章
📜  通过用它们的总和替换相邻的数组元素对,使所有数组元素均匀

📅  最后修改于: 2021-09-06 06:49:35             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是通过用它们的总和替换一对相邻元素来生成所有数组元素。

例子:

方法:这个想法是利用两个奇数之和产生一个偶数的事实。请按照以下步骤解决问题:

  1. 初始化两个整数,比如res来计算替换的次数,以及odd_continuous_segment来计算连续奇数的数量
  2. 遍历数组并检查每个数组元素的以下条件:
    1. 如果arr[i]是奇数,则将odd_continuous_segment的计数1
    2. 否则,如果odd_continuous_segment是奇数,则将res增加odd_continuous_segment/2 。否则,将res增加odd_continuous_segment / 2 + 2并将odd_continuous_segment分配给0
  3. 检查odd_continuous_segment是否为奇数。如果发现为真,则将res增加odd_continuous_segment / 2 。否则将res增加(odd_continuous_segment / 2 + 2)
  4. 最后打印得到的res

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find minimum count of operations
// required to make all array elements even
int make_array_element_even(int arr[], int N)
{
    // Stores minimum count of replacements
    // to make all array elements even
    int res = 0;
 
    // Stores the count of odd
    // continuous numbers
    int odd_cont_seg = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is an odd number
        if (arr[i] % 2 == 1) {
 
            // Update odd_cont_seg
            odd_cont_seg++;
        }
        else {
 
            if (odd_cont_seg > 0) {
 
                // If odd_cont_seg is even
                if (odd_cont_seg % 2 == 0) {
 
                    // Update res
                    res += odd_cont_seg / 2;
                }
 
                else {
 
                    // Update res
                    res += (odd_cont_seg / 2) + 2;
                }
 
                // Reset odd_cont_seg = 0
                odd_cont_seg = 0;
            }
        }
    }
 
    // If odd_cont_seg exceeds 0
    if (odd_cont_seg > 0) {
 
        // If odd_cont_seg is even
        if (odd_cont_seg % 2 == 0) {
 
            // Update res
            res += odd_cont_seg / 2;
        }
 
        else {
 
            // Update res
            res += odd_cont_seg / 2 + 2;
        }
    }
 
    // Print the result
    return res;
}
 
// Drivers Code
int main()
{
    int arr[] = { 2, 4, 5, 11, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << make_array_element_even(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
  // Function to find minimum count of operations
  // required to make all array elements even
  static int make_array_element_even(int arr[], int N)
  {
 
    // Stores minimum count of replacements
    // to make all array elements even
    int res = 0;
 
    // Stores the count of odd
    // continuous numbers
    int odd_cont_seg = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
      // If arr[i] is an odd number
      if (arr[i] % 2 == 1)
      {
 
        // Update odd_cont_seg
        odd_cont_seg++;
      }
      else
      {
        if (odd_cont_seg > 0)
        {
 
          // If odd_cont_seg is even
          if (odd_cont_seg % 2 == 0)
          {
 
            // Update res
            res += odd_cont_seg / 2;
          }
 
          else
          {
 
            // Update res
            res += (odd_cont_seg / 2) + 2;
          }
 
          // Reset odd_cont_seg = 0
          odd_cont_seg = 0;
        }
      }
    }
 
    // If odd_cont_seg exceeds 0
    if (odd_cont_seg > 0)
    {
 
      // If odd_cont_seg is even
      if (odd_cont_seg % 2 == 0)
      {
 
        // Update res
        res += odd_cont_seg / 2;
      }
 
      else
      {
 
        // Update res
        res += odd_cont_seg / 2 + 2;
      }
    }
 
    // Print the result
    return res;
  }
 
  // Drivers Code
  public static void main(String[] args)
  {
    int arr[] = { 2, 4, 5, 11, 6 };
    int N = arr.length;
    System.out.print(make_array_element_even(arr, N));
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python program to implement
# the above approach
 
# Function to find minimum count of operations
# required to make all array elements even
def make_array_element_even(arr, N):
     
    # Stores minimum count of replacements
    # to make all array elements even
    res = 0
     
    # Stores the count of odd
    # continuous numbers
    odd_cont_seg = 0
     
    # Traverse the array
    for i in range(0, N):
         
        # If arr[i] is an odd number
        if (arr[i] % 2 == 1):
           
            # Update odd_cont_seg
            odd_cont_seg+=1
        else:
            if (odd_cont_seg > 0):
               
                # If odd_cont_seg is even
                if (odd_cont_seg % 2 == 0):
                   
                    # Update res
                    res += odd_cont_seg // 2
                 
                else:
                   
                    # Update res
                    res += (odd_cont_seg // 2) + 2
                 
                # Reset odd_cont_seg = 0
                odd_cont_seg = 0
     
    # If odd_cont_seg exceeds 0
    if (odd_cont_seg > 0):
         
        # If odd_cont_seg is even
        if (odd_cont_seg % 2 == 0):
             
            # Update res
            res += odd_cont_seg // 2
         
        else:
             
            # Update res
            res += odd_cont_seg // 2 + 2
             
    # Prthe result
    return res
 
# Drivers Code
arr =  [2, 4, 5, 11, 6]
N = len(arr)
print(make_array_element_even(arr, N))
 
# This code is contributed by shubhamsingh10


C#
// C# program to implement
// the above approach
using System;
public class GFG
{
 
  // Function to find minimum count of operations
  // required to make all array elements even
  static int make_array_element_even(int []arr, int N)
  {
 
    // Stores minimum count of replacements
    // to make all array elements even
    int res = 0;
 
    // Stores the count of odd
    // continuous numbers
    int odd_cont_seg = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
      // If arr[i] is an odd number
      if (arr[i] % 2 == 1)
      {
 
        // Update odd_cont_seg
        odd_cont_seg++;
      }
      else
      {
        if (odd_cont_seg > 0)
        {
 
          // If odd_cont_seg is even
          if (odd_cont_seg % 2 == 0)
          {
 
            // Update res
            res += odd_cont_seg / 2;
          }
 
          else
          {
 
            // Update res
            res += (odd_cont_seg / 2) + 2;
          }
 
          // Reset odd_cont_seg = 0
          odd_cont_seg = 0;
        }
      }
    }
 
    // If odd_cont_seg exceeds 0
    if (odd_cont_seg > 0)
    {
 
      // If odd_cont_seg is even
      if (odd_cont_seg % 2 == 0)
      {
 
        // Update res
        res += odd_cont_seg / 2;
      }
 
      else
      {
 
        // Update res
        res += odd_cont_seg / 2 + 2;
      }
    }
 
    // Print the result
    return res;
  }
 
  // Drivers Code
  public static void Main(String[] args)
  {
    int []arr = { 2, 4, 5, 11, 6 };
    int N = arr.Length;
    Console.Write(make_array_element_even(arr, N));
  }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
1

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live