# Check if all the elements can be made of same parity by inverting adjacent elements

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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

Yes

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum operations required to modify the array such that parity of adjacent elements is different
- Check if matrix A can be converted to B by changing parity of corner elements of any submatrix
- Check if all the elements can be made equal on dividing with X and Y
- Check if all elements of binary array can be made 1
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Query to check if a range is made up of consecutive elements
- Check if all elements of the given array can be made 0 by decrementing value in pairs
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Maximum Subarray Sum after inverting at most two elements
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Count of array elements which is smaller than both its adjacent elements
- Minimum elements to be removed such that sum of adjacent elements is always even
- Minimum elements to be removed such that sum of adjacent elements is always odd
- Sum of elements from an array having even parity
- Query to count odd and even parity elements in subarray after XOR with K
- Count of all possible pairs of array elements with same parity
- Count of odd and even parity elements in subarray using MO's algorithm
- Longest subarray whose elements can be made equal by maximum K increments
- Minimize the count of adjacent pairs with different parity

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.