Minimize increments required to make count of even and odd array elements equal
Last Updated :
15 Sep, 2022
Given an array arr[] of size N, the task is to find the minimum increments by 1 required to be performed on the array elements such that the count of even and odd integers in the given array becomes equal. If it is not possible, then print “-1”.
Examples:
Input: arr[] = {1, 3, 4, 9}
Output: 1
Explanation:
Count of even and odd integers in the array are 1 and 3 respectively.
Increment arr[3] ( = 9) by 1 to make it 10(even).
So, as the count of even and odd integer are the same after the above steps. Hence, the minimum increment operations is 1.
Input: arr[] = {2, 2, 2, 2}
Output: 2
Approach: The idea to solve the given problem is as follows:
- If N is even, then traverse the array and keep a count of odd and even integers. The absolute difference of the count of even and odd integers divided by 2 gives the minimum increment operations required to make even and odd numbers equal.
- If N is odd, then it is not possible to make even and odd numbers equal, hence print “-1”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumIncrement( int arr[], int N)
{
if (N % 2 != 0) {
cout << "-1" ;
exit (0);
}
int cntEven = 0;
int cntOdd = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] % 2 == 0) {
cntEven += 1;
}
}
cntOdd = N - cntEven;
return abs (cntEven - cntOdd) / 2;
}
int main()
{
int arr[] = { 1, 3, 4, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minimumIncrement(arr, N);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static int minimumIncrement( int arr[], int N)
{
if (N % 2 != 0 )
{
System.out.println( "-1" );
System.exit( 0 );
}
int cntEven = 0 ;
int cntOdd = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] % 2 == 0 )
{
cntEven += 1 ;
}
}
cntOdd = N - cntEven;
return Math.abs(cntEven - cntOdd) / 2 ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 4 , 9 };
int N = arr.length;
System.out.println(minimumIncrement(arr, N));
}
}
|
Python3
def minimumIncrement(arr, N):
if (N % 2 ! = 0 ):
print ( "-1" )
return
cntEven = 0
cntOdd = 0
for i in range (N):
if (arr[i] % 2 = = 0 ):
cntEven + = 1
cntOdd = N - cntEven
return abs (cntEven - cntOdd) / / 2
if __name__ = = '__main__' :
arr = [ 1 , 3 , 4 , 9 ]
N = len (arr)
print (minimumIncrement(arr, N))
|
C#
using System;
class GFG
{
static int minimumIncrement( int [] arr, int N)
{
if (N % 2 != 0)
{
Console.WriteLine( "-1" );
Environment.Exit(0);
}
int cntEven = 0;
int cntOdd = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] % 2 == 0)
{
cntEven += 1;
}
}
cntOdd = N - cntEven;
return Math.Abs(cntEven - cntOdd) / 2;
}
public static void Main()
{
int [] arr = { 1, 3, 4, 9 };
int N = arr.Length;
Console.WriteLine(minimumIncrement(arr, N));
}
}
|
Javascript
<script>
function minimumIncrement(arr , N) {
if (N % 2 != 0) {
document.write( "-1" );
System.exit(0);
}
var cntEven = 0;
var cntOdd = 0;
for (i = 0; i < N; i++) {
if (arr[i] % 2 == 0) {
cntEven += 1;
}
}
cntOdd = N - cntEven;
return Math.abs(cntEven - cntOdd) / 2;
}
var arr = [ 1, 3, 4, 9 ];
var N = arr.length;
document.write(minimumIncrement(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...