📌  相关文章
📜  最多一次转换,最大化给定数组中奇数对的数量

📅  最后修改于: 2022-05-13 01:56:09.067000             🧑  作者: Mango

最多一次转换,最大化给定数组中奇数对的数量

给定一个大小为 n的整数数组,通过最多更改一个数字,从数组中找到最大对数,使得它们的和为奇数。

例子:

方法:要使元素之和为奇数,一个数应为奇数,另一个数应为偶数。可以按照以下步骤进行:

  • 计算x等于奇数元素的数量
  • 计算 Y 等于偶数元素的数量。
  • 用 x 和 y 的最小值初始化 answer 变量
  • 如果 |x – y| >= 2,然后将答案加 1(即,如果 y – x >= 2 则我们可以将一个奇数转换为偶数,如果 x – y >= 2 那么我们可以将一个偶数转换为奇数)
  • 结果答案变量是我们需要的答案。

下面是上述方法的实现:

C++
// C++ code implementation for above approach
#include 
using namespace std;
 
// To find maximum number of pairs in
// array with conversion of at most one element
int maximumNumberofpairs(int n, int arr[])
{
    // Initialize count of even elements
    int x = 0;
 
    // Initialize count of odd elements
    int y = 0;
    for (int i = 0; i < n; i++) {
 
        // If current number is even
        // then increment x by 1
        if (arr[i] % 2 == 0) {
            x++;
        }
 
        // If current number is odd
        // then increment y by 1
        else {
            y++;
        }
    }
 
    // Initialize the answer by min(x, y)
    int answer = min(x, y);
 
    // If difference in count of odd and even
    // is more than 2 than increment answer
    if (abs(x - y) >= 2) {
        answer++;
    }
 
    // Return final answer
    return answer;
}
// Driver code
int main()
{
    // Given array
    int arr[] = { 1, 2, 4, 6, 5, 10, 12 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << maximumNumberofpairs(n, arr) << endl;
 
    return 0;
}


Java
// Java implementation for the above approach
import java.io.*;
 
class GFG
 {
 
  // To find maximum number of pairs in
// array with conversion of at most one element
static int maximumNumberofpairs(int n, int arr[])
{
 
    // Initialize count of even elements
    int x = 0;
   
    // Initialize count of odd elements
    int y = 0;
    for (int i = 0; i < n; i++) {
   
        // If current number is even
        // then increment x by 1
        if (arr[i] % 2 == 0) {
            x++;
        }
   
        // If current number is odd
        // then increment y by 1
        else {
            y++;
        }
    }
   
    // Initialize the answer by min(x, y)
    int answer = Math.min(x, y);
   
    // If difference in count of odd and even
    // is more than 2 than increment answer
    if (Math.abs(x - y) >= 2) {
        answer++;
    }
   
    // Return final answer
    return answer;
}
 
// Driver code
    public static void main (String[] args) {
      // Given array
    int arr[] = { 1, 2, 4, 6, 5, 10, 12 };
    int n = arr.length;
 
        System.out.println( maximumNumberofpairs(n, arr));
    }
}
 
// This code is contributed by Potta Lokesh


Python
# python 3 code implementation for above approach
 
# To find maximum number of pairs in
# array with conversion of at most one element
def maximumNumberofpairs(n, arr):
    # Initialize count of even elements
    x = 0
 
    # Initialize count of odd elements
    y = 0
    for i in range(n):
        # If current number is even
        # then increment x by 1
        if (arr[i] % 2 == 0):
            x += 1
 
        # If current number is odd
        # then increment y by 1
        else:
            y += 1
 
    # Initialize the answer by min(x, y)
    answer = min(x, y)
 
    # If difference in count of odd and even
    # is more than 2 than increment answer
    if (abs(x - y) >= 2):
        answer += 1
 
    # Return final answer
    return answer
 
# Driver code
if __name__ == '__main__':
    # Given array
    arr = [1, 2, 4, 6, 5, 10, 12]
    n = len(arr)
    print(maximumNumberofpairs(n, arr))
 
# This code is contributed by ipg2016107.


C#
// C# implementation for the above approach
 
using System;
 
public class GFG
 {
 
  // To find maximum number of pairs in
// array with conversion of at most one element
static int maximumNumberofpairs(int n, int []arr)
{
 
    // Initialize count of even elements
    int x = 0;
   
    // Initialize count of odd elements
    int y = 0;
    for (int i = 0; i < n; i++) {
   
        // If current number is even
        // then increment x by 1
        if (arr[i] % 2 == 0) {
            x++;
        }
   
        // If current number is odd
        // then increment y by 1
        else {
            y++;
        }
    }
   
    // Initialize the answer by min(x, y)
    int answer = Math.Min(x, y);
   
    // If difference in count of odd and even
    // is more than 2 than increment answer
    if (Math.Abs(x - y) >= 2) {
        answer++;
    }
   
    // Return final answer
    return answer;
}
 
// Driver code
    public static void Main (string[] args) {
     
    // Given array
    int []arr = { 1, 2, 4, 6, 5, 10, 12 };
    int n = arr.Length;
 
        Console.WriteLine(maximumNumberofpairs(n, arr));
    }
}
 
// This code is contributed by AnkThon


JavaScript


输出:
3

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