# Maximize count of odd-sum pairs in given Array with at most one conversion

Last Updated : 23 Dec, 2023

Given an array of integers size n, find maximum number of pairs from array such that their sum is odd, by changing at most one number.

Example:

Input: N = 6, arr = [1, 5, 3, 6, 8, 0]
Output: 3
Explanation: There are 3 even and 3 odd numbers

Input: N = 8, arr = [1, 5, 3, 6, 8, 0, 2, 4]
Output: 4
Explanation: There are 5 even and 3 odd numbers, and an even number can be converted to an odd number. Hence final output will be 4.

Approach: For sum of element to be odd, one number should be odd and other number should be even. Below steps can be followed:

• Calculate x which is equal to number of elements which are odd
• Calculate Y which is equal to number of element which are even.
• initialize the answer variable by minimum of x and y
• if |x – y| >= 2, then increment answer by 1 (i.e., if y – x >= 2 then we can convert one odd number into even and if x – y >= 2 then we can convert one even number into odd number)

Below is the implementation of the above approach:

## C++

 `#include `   `// To find the 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 the current number is even, increment x by 1` `        ``if` `(arr[i] % 2 == 0) {` `            ``x++;` `        ``}` `        ``// If the current number is odd, increment y by 1` `        ``else` `{` `            ``y++;` `        ``}` `    ``}` `    ``// Initialize the answer by min(x, y)` `    ``int` `answer = std::min(x, y);` `    ``// If the difference in count of odd and even` `    ``// is more than 2, then increment the answer` `    ``if` `(std::``abs``(x - y) >= 2) {` `        ``answer++;` `    ``}` `    ``// Return the 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]);` `    ``std::cout << maximumNumberofpairs(n, arr) << std::endl;` `    ``return` `0;` `}`

## Java

 `import` `java.io.*;`   `class` `GFG {` `    ``// To find the 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 the current number is even, increment x by 1` `            ``if` `(arr[i] % ``2` `== ``0``) {` `                ``x++;` `            ``}` `            ``// If the current number is odd, increment y by 1` `            ``else` `{` `                ``y++;` `            ``}` `        ``}` `        ``// Initialize the answer by min(x, y)` `        ``int` `answer = Math.min(x, y);` `        ``// If the difference in count of odd and even` `        ``// is more than 2, then increment the answer` `        ``if` `(Math.abs(x - y) >= ``2``) {` `            ``answer++;` `        ``}` `        ``// Return the 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));` `    ``}` `}`

## Python3

 `# To find the 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 the current number is even, increment x by 1` `        ``if` `arr[i] ``%` `2` `=``=` `0``:` `            ``x ``+``=` `1` `        ``# If the current number is odd, increment y by 1` `        ``else``:` `            ``y ``+``=` `1` `    ``# Initialize the answer by min(x, y)` `    ``answer ``=` `min``(x, y)` `    ``# If the difference in count of odd and even` `    ``# is more than 2, then increment the answer` `    ``if` `abs``(x ``-` `y) >``=` `2``:` `        ``answer ``+``=` `1` `    ``# Return the 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))`

## C#

 `using` `System;`   `class` `GFG {` `    ``// To find the 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 the current number is even, increment x by 1` `            ``if` `(arr[i] % 2 == 0) {` `                ``x++;` `            ``}` `            ``// If the current number is odd, increment y by 1` `            ``else` `{` `                ``y++;` `            ``}` `        ``}` `        ``// Initialize the answer by min(x, y)` `        ``int` `answer = Math.Min(x, y);` `        ``// If the difference in count of odd and even` `        ``// is more than 2, then increment the answer` `        ``if` `(Math.Abs(x - y) >= 2) {` `            ``answer++;` `        ``}` `        ``// Return the 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));` `    ``}` `}`

## Javascript

 `// To find the maximum number of pairs in` `// array with conversion of at most one element` `function` `maximumNumberofpairs(n, arr) {` `    ``// Initialize count of even elements` `    ``var` `x = 0;` `    ``// Initialize count of odd elements` `    ``var` `y = 0;` `    ``for` `(``var` `i = 0; i < n; i++) {` `        ``// If the current number is even, increment x by 1` `        ``if` `(arr[i] % 2 == 0) {` `            ``x++;` `        ``}` `        ``// If the current number is odd, increment y by 1` `        ``else` `{` `            ``y++;` `        ``}` `    ``}` `    ``// Initialize the answer by min(x, y)` `    ``var` `answer = Math.min(x, y);` `    ``// If the difference in count of odd and even` `    ``// is more than 2, then increment the answer` `    ``if` `(Math.abs(x - y) >= 2) {` `        ``answer++;` `    ``}` `    ``// Return the final answer` `    ``console.log(answer);` `}`   `// Driver code` `// Given array` `var` `arr = [1, 2, 4, 6, 5, 10, 12];` `var` `n = arr.length;` `maximumNumberofpairs(n, arr)`

Output

```3

```

Time complexity: O(N)
Auxiliary Space: O(1)

Previous
Next