Check if all the elements can be made of same parity by inverting adjacent elements Last Updated : 22 Nov, 2021 Comments Improve Suggest changes Like Article Like Report Given a binary matrix. In a single operation, you are allowed to choose two adjacent elements and invert their parity. The operation can be performed any number of times. Write a program to check if all the elements of the array can be converted into a single parity. Examples: Input: a[] = {1, 0, 1, 1, 0, 1} Output: Yes Invert 2nd and 3rd elements to get {1, 1, 0, 1, 0, 1} Invert 3rd and 4th elements to get {1, 1, 1, 0, 0, 1} Invert 4th and 5th elements to get {1, 1, 1, 1, 1, 1}Input: a[] = {1, 1, 1, 0, 0, 0} Output: No Approach: Since only adjacent elements are needed to be flipped, hence the count of parities will give the answer to the question. Only even number of elements are flipped at a time, so if both the parity's count is odd then it is not possible to make all the parity same else it is possible.Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to check if parity // can be made same or not bool flipsPossible(int a[], int n) { int count_odd = 0, count_even = 0; // Iterate and count the parity for (int i = 0; i < n; i++) { // Odd if (a[i] & 1) count_odd++; // Even else count_even++; } // Condition check if (count_odd % 2 && count_even % 2) return false; else return true; } // Drivers code int main() { int a[] = { 1, 0, 1, 1, 0, 1 }; int n = sizeof(a) / sizeof(a[0]); if (flipsPossible(a, n)) cout << "Yes"; else cout << "No"; return 0; } Java // Java implementation of the approach public class GFG { // Function to check if parity // can be made same or not static boolean flipsPossible(int []a, int n) { int count_odd = 0, count_even = 0; // Iterate and count the parity for (int i = 0; i < n; i++) { // Odd if ((a[i] & 1) == 1) count_odd++; // Even else count_even++; } // Condition check if (count_odd % 2 == 1 && count_even % 2 == 1) return false; else return true; } // Drivers code public static void main (String[] args) { int []a = { 1, 0, 1, 1, 0, 1 }; int n = a.length; if (flipsPossible(a, n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by AnkitRai01 Python3 # Python3 implementation of the approach # Function to check if parity # can be made same or not def flipsPossible(a, n) : count_odd = 0; count_even = 0; # Iterate and count the parity for i in range(n) : # Odd if (a[i] & 1) : count_odd += 1; # Even else : count_even += 1; # Condition check if (count_odd % 2 and count_even % 2) : return False; else : return True; # Driver Code if __name__ == "__main__" : a = [ 1, 0, 1, 1, 0, 1 ]; n = len(a); if (flipsPossible(a, n)) : print("Yes"); else : print("No"); # This code is contributed by AnkitRai01 C# // C# implementation of the approach using System; class GFG { // Function to check if parity // can be made same or not static bool flipsPossible(int []a, int n) { int count_odd = 0, count_even = 0; // Iterate and count the parity for (int i = 0; i < n; i++) { // Odd if ((a[i] & 1) == 1) count_odd++; // Even else count_even++; } // Condition check if (count_odd % 2 == 1 && count_even % 2 == 1) return false; else return true; } // Drivers code public static void Main(String[] args) { int []a = { 1, 0, 1, 1, 0, 1 }; int n = a.Length; if (flipsPossible(a, n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by 29AjayKumar JavaScript <script> // JavaScript implementation of the approach // Function to check if parity // can be made same or not function flipsPossible(a, n){ let count_odd = 0; let count_even = 0; // Iterate and count the parity for (let i = 0; i < n; i++) { // Odd if ((a[i] & 1) == 1) count_odd++; // Even else count_even++; } // Condition check if (count_odd % 2 == 1 && count_even % 2 == 1) return false; else return true; } // Drivers code let a = [1, 0, 1, 1, 0, 1]; let n = a.length; if (flipsPossible(a, n)) document.write("Yes"); else document.write("No"); </script> OutputYes Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if all the elements can be made of same parity by inverting adjacent elements S Striver Follow Improve Article Tags : Analysis of Algorithms Matrix DSA Arrays binary-representation +1 More Practice Tags : ArraysMatrix Similar Reads Check if all elements of a Circular Array can be made equal by increments of adjacent pairs Given a circular array arr[] of size N, the task is to check if it is possible to make all array elements of the circular array equal by increasing pairs of adjacent elements by 1. Examples: Input: N = 4, arr[] = {2, 1, 3, 4} Output:YesExplanation:Step 1: {2, 1, 3, 4} -> {3, 2, 3, 4}Step 2: {3, 2 5 min read Make all array elements even by replacing adjacent pair of array elements with their sum Given an array arr[] of size N, the task is to make all array elements even by replacing a pair of adjacent elements with their sum. Examples: Input: arr[] = { 2, 4, 5, 11, 6 }Output: 1Explanation:Replacing a pair (arr[2], arr[3]) with their sum ( = 5 + 11 = 16) modifies arr[] to { 2, 4, 16, 16, 6 } 8 min read Check if array can be sorted by swapping adjacent elements of opposite parity Given an array A of size n, the task is to check if the array can be sorted in increasing order, if the only operation allowed is swapping the adjacent elements if they are of opposite parity. The operation can be done any number of times. Examples: Input : n = 4, A = [1, 6, 51, 16]Output: YESExplan 9 min read Count of all subsequences having adjacent elements with different parity Given an array arr[] of size N, the task is to find the number of non-empty subsequences from the given array such that no two adjacent elements of the subsequence have the same parity. Examples: Input: arr[] = [5, 6, 9, 7] Output: 9 Explanation: All such subsequences of given array will be {5}, {6} 9 min read Check if an array can be sorted by swapping adjacent elements such that each element is swapped even number of times Given an array arr[] consisting of N integers, the task is to check if the array can be sorted by swapping adjacent elements any number of times such that each array element is swapped even a number of times. Examples: Input: arr[] = {4, 3, 2, 5}Output: YesExplanation:Below are the possible order of 8 min read Check if Array can be sorted by swapping adjacent elements having odd sum Given an array arr[], the task is to check if the array can be sorted using the given operation any number of times. In one operation, you can swap any two adjacent elements if their sum is odd. Examples: Input: arr[] = [1, 6, 31, 14]Output: YesExplanation: Swap 31 and 14 (31 + 14 = 45 which is odd) 7 min read Minimum elements to be removed such that sum of adjacent elements is always odd Given an array of N integers. The task is to eliminate the minimum number of elements such that in the resulting array the sum of any two adjacent values is odd. Examples: Input: arr[] = {1, 2, 3} Output: 0 Sum of all adjacent elements is already odd. Input: arr[] = {1, 3, 5, 4, 2} Output: 3 Elimina 5 min read Check if matrix A can be converted to B by changing parity of corner elements of any submatrix Given two binary matrices, A[][] and B[][] of NÃM. In a single operation, one can choose a sub-matrix (min of 2 rows and 2c columns) and change the parity of the corner elements i.e. 1 can be changed to a 0, and 0 can be changed to a 1. The task is to check if matrix A can be converted to B using an 8 min read Check if even and odd count of elements can be made equal in Array Given an array Arr[] of N integers and an integer K, the task is to find if it is possible to make the count of even and odd elements equal by performing the following operations at most K times: Choose any index i such that Arr[i] is even and divide it by 2.Choose any index i such that Arr[i] is od 9 min read Find index of the element differing in parity with all other array elements Given an array arr[] of size N (N > 3), the task is to find the position of the element that differs in parity (odd/even) with respect to all other array elements. Note: It is guaranteed that there will always be a number that differs in parity from all other elements. Examples: Input: arr[] = {2 11 min read Like