Related Articles

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

• Difficulty Level : Medium
• Last Updated : 06 Sep, 2021

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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// 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);``    ``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

 ``
Output:
```3
```

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up