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 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)
- resultant answer variable is our required answer.
Below is the implementation of the above approach:
C++
#include <iostream>
int maximumNumberofpairs( int n, int arr[]) {
int x = 0;
int y = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
x++;
}
else {
y++;
}
}
int answer = std::min(x, y);
if (std:: abs (x - y) >= 2) {
answer++;
}
return answer;
}
int main() {
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 {
static int maximumNumberofpairs( int n, int arr[]) {
int x = 0 ;
int y = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 == 0 ) {
x++;
}
else {
y++;
}
}
int answer = Math.min(x, y);
if (Math.abs(x - y) >= 2 ) {
answer++;
}
return answer;
}
public static void main(String[] args) {
int arr[] = { 1 , 2 , 4 , 6 , 5 , 10 , 12 };
int n = arr.length;
System.out.println(maximumNumberofpairs(n, arr));
}
}
|
Python3
def maximumNumberofpairs(n, arr):
x = 0
y = 0
for i in range (n):
if arr[i] % 2 = = 0 :
x + = 1
else :
y + = 1
answer = min (x, y)
if abs (x - y) > = 2 :
answer + = 1
return answer
if __name__ = = '__main__' :
arr = [ 1 , 2 , 4 , 6 , 5 , 10 , 12 ]
n = len (arr)
print (maximumNumberofpairs(n, arr))
|
C#
using System;
class GFG {
static int maximumNumberofpairs( int n, int [] arr) {
int x = 0;
int y = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
x++;
}
else {
y++;
}
}
int answer = Math.Min(x, y);
if (Math.Abs(x - y) >= 2) {
answer++;
}
return answer;
}
public static void Main( string [] args) {
int [] arr = {1, 2, 4, 6, 5, 10, 12};
int n = arr.Length;
Console.WriteLine(maximumNumberofpairs(n, arr));
}
}
|
Javascript
function maximumNumberofpairs(n, arr) {
var x = 0;
var y = 0;
for ( var i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
x++;
}
else {
y++;
}
}
var answer = Math.min(x, y);
if (Math.abs(x - y) >= 2) {
answer++;
}
console.log(answer);
}
var arr = [1, 2, 4, 6, 5, 10, 12];
var n = arr.length;
maximumNumberofpairs(n, arr)
|
Time complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
23 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...