Related Articles

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

• Last Updated : 24 May, 2021

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

 ``
Output:
`Yes`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up