Check if all the elements can be made of same parity by inverting adjacent elements
Last Updated :
22 Nov, 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++
#include <bits/stdc++.h>
using namespace std;
bool flipsPossible( int a[], int n)
{
int count_odd = 0, count_even = 0;
for ( int i = 0; i < n; i++) {
if (a[i] & 1)
count_odd++;
else
count_even++;
}
if (count_odd % 2 && count_even % 2)
return false ;
else
return true ;
}
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
public class GFG
{
static boolean flipsPossible( int []a, int n)
{
int count_odd = 0 , count_even = 0 ;
for ( int i = 0 ; i < n; i++)
{
if ((a[i] & 1 ) == 1 )
count_odd++;
else
count_even++;
}
if (count_odd % 2 == 1 && count_even % 2 == 1 )
return false ;
else
return true ;
}
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" );
}
}
|
Python3
def flipsPossible(a, n) :
count_odd = 0 ; count_even = 0 ;
for i in range (n) :
if (a[i] & 1 ) :
count_odd + = 1 ;
else :
count_even + = 1 ;
if (count_odd % 2 and count_even % 2 ) :
return False ;
else :
return True ;
if __name__ = = "__main__" :
a = [ 1 , 0 , 1 , 1 , 0 , 1 ];
n = len (a);
if (flipsPossible(a, n)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static bool flipsPossible( int []a, int n)
{
int count_odd = 0, count_even = 0;
for ( int i = 0; i < n; i++)
{
if ((a[i] & 1) == 1)
count_odd++;
else
count_even++;
}
if (count_odd % 2 == 1 && count_even % 2 == 1)
return false ;
else
return true ;
}
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" );
}
}
|
Javascript
<script>
function flipsPossible(a, n){
let count_odd = 0;
let count_even = 0;
for (let i = 0; i < n; i++)
{
if ((a[i] & 1) == 1)
count_odd++;
else
count_even++;
}
if (count_odd % 2 == 1 && count_even % 2 == 1)
return false ;
else
return true ;
}
let a = [1, 0, 1, 1, 0, 1];
let n = a.length;
if (flipsPossible(a, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...