Related Articles

# Minimum increments required to make absolute difference of all pairwise adjacent array elements even

• Last Updated : 19 May, 2021

Given an array arr[] consisting of N integers, the task is to find the minimum number of array elements required to be incremented to make the absolute difference between all pairwise consecutive elements even.

Examples:

Input: arr[] = {2, 4, 3, 1, 8}
Output: 2
Explanation:
Operation 1: Incrementing the array element arr(= 3) modifies the array to {2, 4, 4, 1, 8}.
Operation 2: Incrementing the array element arr(= 1) modifies the array to {2, 4, 4, 2, 8}.
Therefore, the difference between all pairwise adjacent array elements is even.

Input: arr[] = {1, 3, 5, 2}
Output: 1

Approach: The given problem can be solved by using the fact that the difference between two numbers is even if and only if both numbers are odd or even. Therefore, the idea is to increase either all the odd or even numbers. Both numbers are even and, for the minimum count of increment, print the minimum of the count of odd numbers or count of even numbers is a result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the minimum number``// of increments of array elements``// required to make difference between``// all pairwise adjacent elements even``int` `minOperations(``int` `arr[], ``int` `n)``{``    ``// Stores the count of``    ``// odd and even elements``    ``int` `oddcount = 0, evencount = 0;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Increment odd count``        ``if` `(arr[i] % 2 == 1)``            ``oddcount++;` `        ``// Increment even count``        ``else``            ``evencount++;``    ``}` `    ``// Return the minimum number``    ``// of operations required``    ``return` `min(oddcount, evencount);``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 2, 4, 3, 1, 8 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``cout << minOperations(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``import` `java.lang.*;` `class` `GFG{``    ` `// Function to find the minimum number``// of increments of array elements``// required to make difference between``// all pairwise adjacent elements even``static` `int` `minOperations(``int` `arr[], ``int` `n)``{``    ` `    ``// Stores the count of``    ``// odd and even elements``    ``int` `oddcount = ``0``, evencount = ``0``;` `    ``// Traverse the array``    ``for``(``int` `i = ``0``; i < n; i++)``    ``{``        ` `        ``// Increment odd count``        ``if` `(arr[i] % ``2` `== ``1``)``            ``oddcount++;` `        ``// Increment even count``        ``else``            ``evencount++;``    ``}` `    ``// Return the minimum number``    ``// of operations required``    ``return` `Math.min(oddcount, evencount);``}` `// Driver code``public` `static` `void` `main (String[] args)``{``    ``int` `arr[] = { ``2``, ``4``, ``3``, ``1``, ``8` `};``    ``int` `N = arr.length;``    ` `    ``System.out.println(minOperations(arr, N));``}``}` `// This code is contributed by offbeat`

## Python3

 `# Python3 program for the above approach` `# Function to find the minimum number``# of increments of array elements``# required to make difference between``# all pairwise adjacent elements even``def` `minOperations(arr, n):``    ` `    ``# Stores the count of``    ``# odd and even elements``    ``oddcount, evencount ``=` `0``, ``0` `    ``# Traverse the array``    ``for` `i ``in` `range``(n):``        ` `        ``# Increment odd count``        ``if` `(arr[i] ``%` `2` `=``=` `1``):``            ``oddcount ``+``=` `1``            ` `        ``# Increment even count``        ``else``:``            ``evencount ``+``=` `1` `    ``# Return the minimum number``    ``# of operations required``    ``return` `min``(oddcount, evencount)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``arr ``=` `[ ``2``, ``4``, ``3``, ``1``, ``8` `]``    ``N ``=` `len``(arr)``    ` `    ``print` `(minOperations(arr, N))` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG {` `    ``// Function to find the minimum number``    ``// of increments of array elements``    ``// required to make difference between``    ``// all pairwise adjacent elements even``    ``static` `int` `minOperations(``int``[] arr, ``int` `n)``    ``{``        ``// Stores the count of``        ``// odd and even elements``        ``int` `oddcount = 0, evencount = 0;` `        ``// Traverse the array``        ``for` `(``int` `i = 0; i < n; i++) {` `            ``// Increment odd count``            ``if` `(arr[i] % 2 == 1)``                ``oddcount++;` `            ``// Increment even count``            ``else``                ``evencount++;``        ``}` `        ``// Return the minimum number``        ``// of operations required``        ``return` `Math.Min(oddcount, evencount);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int``[] arr = { 2, 4, 3, 1, 8 };``        ``int` `N = (arr.Length);``        ``Console.WriteLine(minOperations(arr, N));``    ``}``}` `// This code is contributed by ukasp.`

## Javascript

 ``
Output:
`2`

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

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