Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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[2](= 3) modifies the array to {2, 4, 4, 1, 8}.
Operation 2: Incrementing the array element arr[3](= 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 <bits/stdc++.h>
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[0]);
    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




<script>
 
// Javascript 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
function minOperations(arr, n)
{
     
    // Stores the count of
    // odd and even elements
    var oddcount = 0, evencount = 0;
 
    // Traverse the array
    for(var 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
var arr = [ 2, 4, 3, 1, 8 ];
var N = arr.length;
 
document.write(minOperations(arr, N));
 
// This code is contributed by Ankita saini
 
</script>


Output: 

2

 

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



Last Updated : 23 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads