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++
// 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; } |
Java
// 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 |
Python3
# 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 |
C#
// 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 |
PHP
<?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 ?> |
1
Recommended Posts:
- Minimum no. of operations required to make all Array Elements Zero
- Minimum operations required to make all the array elements equal
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all Array elements divisible by K
- Minimum count of elements required to obtain the given Array by repeated mirror operations
- Find the minimum number of operations required to make all array elements equal
- Minimum inversions required so that no two adjacent elements are same
- Minimum operations required to remove an array
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Minimum adjacent swaps required to Sort Binary array
- Check if all the elements can be made of same parity by inverting adjacent elements
- Minimum number of given operations required to reduce the array to 0 element
- Minimum number of steps required to obtain the given Array by the given operations
- Count of all subsequences having adjacent elements with different parity
- Minimum increment or decrement operations required to make the array sorted
- Minimum integer that can be obtained by swapping adjacent digits of different parity
- Find the number of operations required to make all array elements Equal
- Minimum absolute difference of adjacent elements in a circular array
- Minimum gcd operations to make all array elements one
- Find minimum changes required in an array for it to contain k distinct elements
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.


