📌  相关文章
📜  即使将任意一对数组元素替换为它们的总和,也可以制作所有数组元素

📅  最后修改于: 2021-05-17 06:17:51             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是即使将任意一对数组元素替换为它们的总和,也要制作所有数组元素。

例子:

方法:这个想法是用两个和数组替换它们的和直到所有的数组元素都是偶数。请按照以下步骤解决问题:

  • 初始化一个变量,例如move ,以存储所需的最少替换次数。
  • 计算给定数组中存在的奇数元素总数,并将其存储在变量中,例如cnt
  • 如果cnt的值是奇数,则打印(cnt / 2 + 2)作为结果。否则,打印cnt / 2作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// of replacements required to make
// all array elements even
void minMoves(int arr[], int N)
{
    // Stores the count of odd elements
    int odd_element_cnt = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Increase count of odd elements
        if (arr[i] % 2 != 0) {
            odd_element_cnt++;
        }
    }
 
    // Store number of replacements required
    int moves = (odd_element_cnt) / 2;
 
    // Two extra moves will be required
    // to make the last odd element even
    if (odd_element_cnt % 2 != 0)
        moves += 2;
 
    // Print the minimum replacements
    cout << moves;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 6, 3, 7, 20 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    minMoves(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the minimum number
// of replacements required to make
// all array elements even
static void minMoves(int arr[], int N)
{
   
    // Stores the count of odd elements
    int odd_element_cnt = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // Increase count of odd elements
        if (arr[i] % 2 != 0)
        {
            odd_element_cnt++;
        }
    }
 
    // Store number of replacements required
    int moves = (odd_element_cnt) / 2;
 
    // Two extra moves will be required
    // to make the last odd element even
    if (odd_element_cnt % 2 != 0)
        moves += 2;
 
    // Print the minimum replacements
    System.out.print(moves);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 5, 6, 3, 7, 20 };
    int N = arr.length;
 
    // Function call
    minMoves(arr, N);
}
}
 
// This code is contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
public class GFG
{
 
  // Function to find the minimum number
  // of replacements required to make
  // all array elements even
  static void minMoves(int []arr, int N)
  {
 
    // Stores the count of odd elements
    int odd_element_cnt = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
      // Increase count of odd elements
      if (arr[i] % 2 != 0)
      {
        odd_element_cnt++;
      }
    }
 
    // Store number of replacements required
    int moves = (odd_element_cnt) / 2;
 
    // Two extra moves will be required
    // to make the last odd element even
    if (odd_element_cnt % 2 != 0)
      moves += 2;
 
    // Print the minimum replacements
    Console.Write(moves);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int []arr = { 5, 6, 3, 7, 20 };
    int N = arr.Length;
 
    // Function call
    minMoves(arr, N);
  }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for the above approach
 
# Function to find the minimum number
# of replacements required to make
# all array elements even
def minMoves(arr, N):
   
    # Stores the count of odd elements
    odd_element_cnt = 0;
 
    # Traverse the array
    for i in range(N):
 
        # Increase count of odd elements
        if (arr[i] % 2 != 0):
            odd_element_cnt += 1;
 
    # Store number of replacements required
    moves = (odd_element_cnt) // 2;
 
    # Two extra moves will be required
    # to make the last odd element even
    if (odd_element_cnt % 2 != 0):
        moves += 2;
 
    # Prthe minimum replacements
    print(moves);
 
# Driver Code
if __name__ == '__main__':
    arr = [5, 6, 3, 7, 20];
    N = len(arr);
 
    # Function call
    minMoves(arr, N);
 
    # This code is contributed by 29AjayKumar


Javascript


输出:
3

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