Open In App

Minimize increments required to make differences between all pairs of array elements even

Last Updated : 15 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array, arr[] consisting of N integers, the task is to minimize the number of increments of array elements required to make all differences pairs of array elements even.

Examples:

Input: arr[] = {4, 1, 2}
Output: 1
Explanation: 
 

Operation 1: Increment arr[1] by 1. The array arr[] modifies to {4, 2, 2}.
All pairs: (4, 2) ? difference = 2
(4, 2) ? difference = 2
(2, 2) ? difference = 0
Now, the pairwise differences between array elements is even. Hence, the answer is 1.

Input: arr[] = {2, 4}
Output: 0
Explanation: Differences between all pairs of array elements is already even. Therefore, the answer is 0.

Approach: The given problem can be solved by observing the fact that, to make the difference between all pairs of array elements even, both the elements must be of the same parity. Therefore, the idea is to convert all the array elements to either even numbers or odd numbers. The minimum number of increments is equal to the minimum of the count of even and odd array elements
 

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 increments
// required to difference between all
// pairs of array elements even
void minimumOperations(int arr[], int N)
{
    // Store the count of odd
    // and even numbers
    int oddCnt = 0, evenCnt = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        if (arr[i] % 2 == 0) {
 
            // Increment evenCnt by 1
            evenCnt++;
        }
        else {
 
            // Increment evenCnt by 1
            oddCnt++;
        }
    }
 
    // Print the minimum of oddCnt
    // and evenCnt
    cout << min(oddCnt, evenCnt);
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 1, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    minimumOperations(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
public class GFG
{
 
  // Function to find the minimum increments
  // required to difference between all
  // pairs of array elements even
  static void minimumOperations(int[] arr, int N)
  {
 
    // Store the count of odd
    // and even numbers
    int oddCnt = 0, evenCnt = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
      if (arr[i] % 2 == 0)
      {
 
        // Increment evenCnt by 1
        evenCnt++;
      }
      else
      {
 
        // Increment oddCnt by 1
        oddCnt++;
      }
    }
 
    // Print the minimum of oddCnt
    // and evenCnt
    System.out.print(Math.min(oddCnt, evenCnt));
  }  
 
  // Driver code
  public static void main(String args[])
  {
    int[] arr = { 4, 1, 2 };
    int N = arr.length;
    minimumOperations(arr, N);
  }
}
 
// This code is contributed by AnkThon


Python3




# Python program for the above approach
 
# Function to find the minimum increments
# required to difference between all
# pairs of array elements even
def minimumOperations(arr, N) :
     
    # Store the count of odd
    # and even numbers
    oddCnt = 0
    evenCnt = 0
 
    # Traverse the array
    for i in range(N):
        if (arr[i] % 2 == 0) :
 
            # Increment evenCnt by 1
            evenCnt += 1
         
        else :
 
            # Increment evenCnt by 1
            oddCnt += 1
         
    # Print minimum of oddCnt
    # and evenCnt
    print(min(oddCnt, evenCnt))
 
# Driver Code
arr = [ 4, 1, 2 ]
N = len(arr)
minimumOperations(arr, N)
 
# This code is contributed by sanjoy_62.


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
     
    // Function to find the minimum increments
    // required to difference between all
    // pairs of array elements even
    static void minimumOperations(int[] arr, int N)
    {
       
        // Store the count of odd
        // and even numbers
        int oddCnt = 0, evenCnt = 0;
       
        // Traverse the array
        for (int i = 0; i < N; i++)
        {
            if (arr[i] % 2 == 0)
            {
       
                // Increment evenCnt by 1
                evenCnt++;
            }
            else
            {
       
                // Increment evenCnt by 1
                oddCnt++;
            }
        }
       
        // Print the minimum of oddCnt
        // and evenCnt
        Console.Write(Math.Min(oddCnt, evenCnt));
    }  
 
  // Driver code
  static void Main()
  {
    int[] arr = { 4, 1, 2 };
    int N = arr.Length;
    minimumOperations(arr, N);
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript




<script>
// Java script program for the above approach
 
 
// Function to find the minimum increments
// required to difference between all
// pairs of array elements even
function minimumOperations(arr,N)
{
 
    // Store the count of odd
    // and even numbers
    let oddCnt = 0, evenCnt = 0;
 
    // Traverse the array
    for (let i = 0; i < N; i++)
    {
    if (arr[i] % 2 == 0)
    {
 
        // Increment evenCnt by 1
        evenCnt++;
    }
    else
    {
 
        // Increment oddCnt by 1
        oddCnt++;
    }
    }
 
    // Print the minimum of oddCnt
    // and evenCnt
    document.write(Math.min(oddCnt, evenCnt));
}
 
// Driver code
 
    let arr = [ 4, 1, 2 ];
    let N = arr.length;
    minimumOperations(arr, N);
 
 
// This code is contributed by Bobby
</script>


Output: 

1

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads