Maximize count of odd-sum pairs in given Array with at most one conversion
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 numbersInput: 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)
- resultant answer variable is our required answer.
Below is the implementation of the above approach:
C++
undefined |
// C++ code implementation for above approach
#include <bits/stdc++.h>
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 then 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 then 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 then 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 then 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
<script>
// JavaScript implementation for the above approach
// To find 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 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)
var answer = Math.min(x, y);
// If difference in count of odd and even
// is more than 2 then increment answer
if (Math.abs(x – y) >= 2) {
answer++;
}
// Return final answer
return answer;
}
// Driver code
// Given array
var arr = [ 1, 2, 4, 6, 5, 10, 12 ];
var n = arr.length;
document.write( maximumNumberofpairs(n, arr));
// This code is contributed by shivanisinghss2110
</script>
3
Time complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...