Reduce a given Binary Array to a single element by removal of Triplets
Last Updated :
30 Jan, 2023
Given an binary array arr[] of size N, the task is to reduce the array to a single element by the following two operations:
- A triplet of consecutive 0‘s or 1‘s remains unchanged.
- A triplet of consecutive array elements consisting of two 0’s and a single 1 or vice versa can be converted to more frequent element.
Examples:
Input: arr[] = {0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1}
Output: No
Explanation:
Following are the operations performed on the array:
{0, 1, 1} -> 1 modifies the array to {1, 1, 1, 0, 0, 1, 1, 1, 1}
{1, 0, 0} -> 0 modifies the array to {1, 1, 0, 1, 1, 1, 1}
{1, 0, 1} -> 1 modifies the array to {1, 1, 1, 1, 1}
Since, all the remaining elements are 1, they remain unchanged.
Therefore, the array cannot be reduced to a single element.
Input: arr[] = {1, 0, 0, 0, 1, 1, 1}
Output: Yes
Explanation:
Following are the operations performed on the array:
{1, 0, 0} -> 0 {0, 0, 1, 1, 1}
{0, 0, 1} -> 0 {0, 1, 1}
{0, 1, 1} -> 1 {1}
Approach:
Follow the steps below to solve the problem:
- Count the frequency of 0‘s and 1‘s.
- Calculate the absolute difference their respective counts.
- If the difference is 1, only then the array can be reduced to 1. Therefore, print Yes.
- Otherwise, print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int arr[], int n)
{
int countzeroes = 0;
int countones = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == 0)
countzeroes++;
else
countones++;
}
if ( abs (countzeroes - countones) == 1)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int arr[] = { 0, 1, 0, 0, 1, 1, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
solve(arr, n);
return 0;
}
|
Java
class GFG{
static void solve( int arr[], int n)
{
int countzeroes = 0 ;
int countones = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] == 0 )
countzeroes++;
else
countones++;
}
if (Math.abs(countzeroes - countones) == 1 )
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main(String[] args)
{
int arr[] = { 0 , 1 , 0 , 0 , 1 , 1 , 1 };
int n = arr.length;
solve(arr, n);
}
}
|
Python3
def solve(arr, n):
countzeroes = 0 ;
countones = 0 ;
for i in range (n):
if (arr[i] = = 0 ):
countzeroes + = 1 ;
else :
countones + = 1 ;
if ( abs (countzeroes - countones) = = 1 ):
print ( "Yes" );
else :
print ( "No" );
if __name__ = = '__main__' :
arr = [ 0 , 1 , 0 , 0 , 1 , 1 , 1 ];
n = len (arr);
solve(arr, n);
|
C#
using System;
class GFG{
static void solve( int []arr, int n)
{
int countzeroes = 0;
int countones = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == 0)
countzeroes++;
else
countones++;
}
if (Math.Abs(countzeroes - countones) == 1)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main(String[] args)
{
int []arr = { 0, 1, 0, 0, 1, 1, 1 };
int n = arr.Length;
solve(arr, n);
}
}
|
Javascript
<script>
function solve(arr, n)
{
var countzeroes = 0;
var countones = 0;
for ( var i = 0; i < n; i++)
{
if (arr[i] == 0)
countzeroes++;
else
countones++;
}
if (Math.abs(countzeroes - countones) == 1)
document.write( "Yes" );
else
document.write( "No" );
}
var arr = [ 0, 1, 0, 0, 1, 1, 1 ];
var n = arr.length;
solve(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...