Minimize increments required to make count of even and odd array elements equal

• Difficulty Level : Basic
• Last Updated : 16 Apr, 2021

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:

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.

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

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find min operations``// to make even and odd count equal``int` `minimumIncrement(``int` `arr[], ``int` `N)``{``    ``// Odd size will never make odd``    ``// and even counts equal``    ``if` `(N % 2 != 0) {``        ``cout << ``"-1"``;``        ``exit``(0);``    ``}` `    ``// Stores the count of even``    ``// numbers in the array arr[]``    ``int` `cntEven = 0;` `    ``// Stores count of odd numbers``    ``// in the array arr[]``    ``int` `cntOdd = 0;` `    ``// Traverse the array arr[]``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// If arr[i] is an``        ``// even number``        ``if` `(arr[i] % 2 == 0) {` `            ``// Update cntEven``            ``cntEven += 1;``        ``}``    ``}` `    ``// Odd numbers in arr[]``    ``cntOdd = N - cntEven;` `    ``// Return absolute difference``    ``// divided by 2``    ``return` `abs``(cntEven - cntOdd) / 2;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 3, 4, 9 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``// Function call``    ``cout << minimumIncrement(arr, N);` `    ``return` `0;``}`

Java

 `// Java program for the above approach``import` `java.util.*;``import` `java.lang.*;``class` `GFG``{``    ` `// Function to find min operations``// to make even and odd count equal``static` `int` `minimumIncrement(``int` `arr[], ``int` `N)``{``  ` `    ``// Odd size will never make odd``    ``// and even counts equal``    ``if` `(N % ``2` `!= ``0``)``    ``{``        ``System.out.println( ``"-1"``);``        ``System.exit(``0``);``    ``}` `    ``// Stores the count of even``    ``// numbers in the array arr[]``    ``int` `cntEven = ``0``;` `    ``// Stores count of odd numbers``    ``// in the array arr[]``    ``int` `cntOdd = ``0``;` `    ``// Traverse the array arr[]``    ``for` `(``int` `i = ``0``; i < N; i++)``    ``{` `        ``// If arr[i] is an``        ``// even number``        ``if` `(arr[i] % ``2` `== ``0``)``        ``{` `            ``// Update cntEven``            ``cntEven += ``1``;``        ``}``    ``}` `    ``// Odd numbers in arr[]``    ``cntOdd = N - cntEven;` `    ``// Return absolute difference``    ``// divided by 2``    ``return` `Math.abs(cntEven - cntOdd) / ``2``;``}``  ` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = { ``1``, ``3``, ``4``, ``9` `};``    ``int` `N = arr.length;` `    ``// Function call``    ``System.out.println(minimumIncrement(arr, N));``}``}` `// This code is contributed by code_hunt.`

Python3

 `# Python3 program for the above approach` `# Function to find min operations``# to make even and odd count equal``def` `minimumIncrement(arr, N):``    ` `    ``# Odd size will never make odd``    ``# and even counts equal``    ``if` `(N ``%` `2` `!``=` `0``):``        ``print``(``"-1"``)``        ``return` `    ``# Stores the count of even``    ``# numbers in the array arr[]``    ``cntEven ``=` `0` `    ``# Stores count of odd numbers``    ``# in the array arr[]``    ``cntOdd ``=` `0` `    ``# Traverse the array arr[]``    ``for` `i ``in` `range``(N):` `        ``# If arr[i] is an``        ``# even number``        ``if` `(arr[i] ``%` `2` `=``=` `0``):` `            ``# Update cntEven``            ``cntEven ``+``=` `1` `    ``# Odd numbers in arr[]``    ``cntOdd ``=` `N ``-` `cntEven` `    ``# Return absolute difference``    ``# divided by 2``    ``return` `abs``(cntEven ``-` `cntOdd) ``/``/` `2` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``1``, ``3``, ``4``, ``9``]``    ``N ``=` `len``(arr)` `    ``# Function call``    ``print` `(minimumIncrement(arr, N))` `    ``# Thiss code is contributed by mohit kumar 29.`

C#

 `// C# program to implement``// the above approach``using` `System;``class` `GFG``{``  ` `// Function to find min operations``// to make even and odd count equal``static` `int` `minimumIncrement(``int``[] arr, ``int` `N)``{``  ` `    ``// Odd size will never make odd``    ``// and even counts equal``    ``if` `(N % 2 != 0)``    ``{``        ``Console.WriteLine( ``"-1"``);``        ``Environment.Exit(0);``    ``}` `    ``// Stores the count of even``    ``// numbers in the array arr[]``    ``int` `cntEven = 0;` `    ``// Stores count of odd numbers``    ``// in the array arr[]``    ``int` `cntOdd = 0;` `    ``// Traverse the array arr[]``    ``for` `(``int` `i = 0; i < N; i++)``    ``{` `        ``// If arr[i] is an``        ``// even number``        ``if` `(arr[i] % 2 == 0)``        ``{` `            ``// Update cntEven``            ``cntEven += 1;``        ``}``    ``}` `    ``// Odd numbers in arr[]``    ``cntOdd = N - cntEven;` `    ``// Return absolute difference``    ``// divided by 2``    ``return` `Math.Abs(cntEven - cntOdd) / 2;``}` `  ``// Driver Code``  ``public` `static` `void`  `Main()``  ``{``    ``int``[] arr = { 1, 3, 4, 9 };``    ``int` `N = arr.Length;` `    ``// Function call``    ``Console.WriteLine(minimumIncrement(arr, N));``  ``}``}` `// This code is contributed by susmitakundugoaldanga.`

Javascript

 ``
Output:
`1`

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

My Personal Notes arrow_drop_up