Minimum operations required to modify the array such that parity of adjacent elements is different

Given an array A[], the task is to find the minimum number of operations required to convert the array into B[] such that for every index in B (except the last) parity(b[i]) != parity(b[i + 1]) where parity(x) = x % 3.

Below is the operation to be performed:

Any element from the set {1, 2} can be added to any element of the array.



Examples:

Input: A[] = {2, 1, 3, 0}
Output: 1
1 can be added to 0 in a single operation and the array becomes {2, 1, 3, 1}.

Input: A[] = {2, 2, 2, 2}
Output: 2

Approach: An optimal approach is to update the element which is in between two other elements so that it can be updated such that its parity can be different from the element previous to it and the element next to it in a single operation (as the number of operations needs to be minimized).
So, start traversing the array from A[1] to A[n – 2] and for every element A[i] such that parity(A[i]) == parity(A[i – 1]) or parity(A[i]) == parity(A[i + 1]), update A[i] such that its parity is different from both the numbers surrounding it and keep a track of the count of operations performed. Print the count in the end.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the parity of a number
int parity(int a)
{
    return a % 3;
}
  
// Function to return the minimum
// number of operations required
int solve(int array[], int size)
{
    int operations = 0;
    for (int i = 0; i < size - 1; i++) {
  
        // Operation needs to be performed
        if (parity(array[i]) == parity(array[i + 1])) {
  
            operations++;
            if (i + 2 < size) {
  
                // Parity of previous element
                int pari1 = parity(array[i]);
  
                // Parity of next element
                int pari2 = parity(array[i + 2]);
  
                // Update parity of current element to be other than
                // the parities of the pervious and the next number
                if (pari1 == pari2) {
                    if (pari1 == 0)
                        array[i + 1] = 1;
                    else if (pari1 == 1)
                        array[i + 1] = 0;
                    else
                        array[i + 1] = 1;
                }
                else {
                    if ((pari1 == 0 && pari2 == 1)
                        || (pari1 == 1 && pari2 == 0))
                        array[i + 1] = 2;
                    if ((pari1 == 1 && pari2 == 2)
                        || (pari1 == 2 && pari2 == 1))
                        array[i + 1] = 0;
                    if ((pari1 == 2 && pari2 == 0)
                        || (pari1 == 0 && pari2 == 2))
                        array[i + 1] = 1;
                }
            }
        }
    }
  
    return operations;
}
  
// Driver Code
int main()
{
    int array[] = { 2, 1, 3, 0 };
    int size = sizeof(array) / sizeof(array[0]);
    cout << solve(array, size) << endl;
  
  return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG 
      
    // Function to return the parity of a number 
    static int parity(int a) 
    
        return a % 3
    
  
    // Function to return the minimum 
    // number of operations required 
    static int solve(int []array, int size) 
    
        int operations = 0
        for (int i = 0; i < size - 1; i++) 
        
      
            // Operation needs to be performed 
            if (parity(array[i]) == parity(array[i + 1])) 
            
      
                operations++; 
                if (i + 2 < size) 
                
      
                    // Parity of previous element 
                    int pari1 = parity(array[i]); 
      
                    // Parity of next element 
                    int pari2 = parity(array[i + 2]); 
      
                    // Update parity of current 
                    // element to be other than 
                    // the parities of the pervious 
                    // and the next number 
                    if (pari1 == pari2) 
                    
                        if (pari1 == 0
                            array[i + 1] = 1
                        else if (pari1 == 1
                            array[i + 1] = 0
                        else
                            array[i + 1] = 1
                    
                    else
                    
                        if ((pari1 == 0 && pari2 == 1) || 
                            (pari1 == 1 && pari2 == 0)) 
                            array[i + 1] = 2
                        if ((pari1 == 1 && pari2 == 2
                            || (pari1 == 2 && pari2 == 1)) 
                            array[i + 1] = 0
                        if ((pari1 == 2 && pari2 == 0
                            || (pari1 == 0 && pari2 == 2)) 
                            array[i + 1] = 1
                    
                
            
        
      
        return operations; 
    
  
    // Driver Code 
    public static void main (String arg[]) 
    
        int []array = { 2, 1, 3, 0 }; 
        int size = array.length; 
        System.out.println(solve(array, size)); 
    
  
// This code is contributed by Ryuga

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the parity 
# of a number 
def parity(a):
    return a % 3
  
# Function to return the minimum 
# number of operations required 
def solve(array, size): 
  
    operations = 0
    for i in range(0, size - 1): 
  
        # Operation needs to be performed 
        if parity(array[i]) == parity(array[i + 1]): 
  
            operations += 1
            if i + 2 < size: 
  
                # Parity of previous element 
                pari1 = parity(array[i]) 
  
                # Parity of next element 
                pari2 = parity(array[i + 2]) 
  
                # Update parity of current element to
                # be other than the parities of the
                # previous and the next number 
                if pari1 == pari2: 
                    if pari1 == 0
                        array[i + 1] = 1
                    elif pari1 == 1
                        array[i + 1] = 0
                    else:
                        array[i + 1] = 1
                  
                else:
                    if ((pari1 == 0 and pari2 == 1) or 
                        (pari1 == 1 and pari2 == 0)): 
                        array[i + 1] = 2
                    if ((pari1 == 1 and pari2 == 2) or 
                        (pari1 == 2 and pari2 == 1)): 
                        array[i + 1] = 0
                    if ((pari1 == 2 and pari2 == 0) and 
                        (pari1 == 0 and pari2 == 2)):
                        array[i + 1] = 1
  
    return operations 
  
# Driver Code 
if __name__ == "__main__"
  
    array = [2, 1, 3, 0
    size = len(array) 
    print(solve(array, size)) 
  
# This code is contributed by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the parity of a number
static int parity(int a)
{
    return a % 3;
}
  
// Function to return the minimum
// number of operations required
static int solve(int []array, int size)
{
    int operations = 0;
    for (int i = 0; i < size - 1; i++)
    {
  
        // Operation needs to be performed
        if (parity(array[i]) == parity(array[i + 1]))
        {
  
            operations++;
            if (i + 2 < size)
            {
  
                // Parity of previous element
                int pari1 = parity(array[i]);
  
                // Parity of next element
                int pari2 = parity(array[i + 2]);
  
                // Update parity of current 
                // element to be other than
                // the parities of the pervious 
                // and the next number
                if (pari1 == pari2) 
                {
                    if (pari1 == 0)
                        array[i + 1] = 1;
                    else if (pari1 == 1)
                        array[i + 1] = 0;
                    else
                        array[i + 1] = 1;
                }
                else
                {
                    if ((pari1 == 0 && pari2 == 1) ||
                        (pari1 == 1 && pari2 == 0))
                        array[i + 1] = 2;
                    if ((pari1 == 1 && pari2 == 2)
                        || (pari1 == 2 && pari2 == 1))
                        array[i + 1] = 0;
                    if ((pari1 == 2 && pari2 == 0)
                        || (pari1 == 0 && pari2 == 2))
                        array[i + 1] = 1;
                }
            }
        }
    }
  
    return operations;
}
  
// Driver Code
public static void Main()
{
    int []array = { 2, 1, 3, 0 };
    int size = array.Length;
    Console.WriteLine(solve(array, size));
}
}
  
// This code is contributed 
// by Akanksha Rai

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to return the parity of a number
function parity($a)
{
    return $a % 3;
}
  
// Function to return the minimum
// number of operations required
function solve($array, $size)
{
    $operations = 0;
    for ($i = 0; $i < $size - 1; $i++) 
    {
  
        // Operation needs to be performed
        if (parity($array[$i]) == parity($array[$i + 1]))
        {
  
            $operations++;
            if ($i + 2 < $size
            {
  
                // Parity of previous element
                $pari1 = parity($array[$i]);
  
                // Parity of next element
                $pari2 = parity($array[$i + 2]);
  
                // Update parity of current element to be 
                // other than the parities of the pervious 
                // and the next number
                if ($pari1 == $pari2)
                {
                    if ($pari1 == 0)
                        $array[$i + 1] = 1;
                    else if ($pari1 == 1)
                        $array[$i + 1] = 0;
                    else
                        $array[$i + 1] = 1;
                }
                else 
                {
                    if (($pari1 == 0 && $pari2 == 1) || 
                        ($pari1 == 1 && $pari2 == 0))
                        $array[$i + 1] = 2;
                    if (($pari1 == 1 && $pari2 == 2) ||
                        ($pari1 == 2 && $pari2 == 1))
                        $array[$i + 1] = 0;
                    if (($pari1 == 2 && $pari2 == 0) || 
                        ($pari1 == 0 && $pari2 == 2))
                        $array[$i + 1] = 1;
                }
            }
        }
    }
  
    return $operations;
}
  
// Driver Code
$array = array(2, 1, 3, 0 );
$size = count($array);
echo solve($array, $size);
  
// This code is contributed by mits
?>

chevron_right


Output:

1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.