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

📅  最后修改于: 2021-09-07 02:18:36             🧑  作者: 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)

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