Binary array after M range toggle operations
Last Updated :
19 Sep, 2023
Consider a binary array consisting of N elements (initially all elements are 0). After that, you are given M commands where each command is of form a b, which means you have to toggle all the elements of the array in range a to b (both inclusive). After the execution of all M commands, you have to find the resultant array.
Examples:
Input : N = 5, M = 3
C1 = 1 3, C2 = 4 5, C3 = 1 4
Output : Resultant array = {0, 0, 0, 0, 1}
Explanation :
Initial array : {0, 0, 0, 0, 0}
After first toggle : {1, 1, 1, 0, 0}
After second toggle : {1, 1, 1, 1, 1}
After third toggle : {0, 0, 0, 0, 1}
Input : N = 5, M = 5
C1 = 1 5, C2 = 1 5, C3 = 1 5,
C4 = 1 5, C5 = 1 5
Output : Resultant array = {1, 1, 1, 1, 1}
Naive Approach: For the given N we should create a bool array of n+1 elements and for each of M commands we have to iterate from a to b and toggle all elements in the range of a to b with help of XOR.
The complexity of this approach is O(n^2).
for (int i = 1; i > a >> b;
for (int j = a; j <= b; j++)
arr[j] ^= 1;
Efficient Approach: The idea is based on the sample problem discussed in the Prefix Sum Array article. For the given n, we create a bool array of n+2 elements and for each of M commands, we have to just toggle elements a and b+1 with help of XOR. After all commands we will process the array as arr[i] ^= arr[i-1];
The complexity of this approach is O(n).
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
void command( bool arr[], int a, int b)
{
arr[a] ^= 1;
arr[b+1] ^= 1;
}
void process( bool arr[], int n)
{
for ( int k=1; k<=n; k++)
arr[k] ^= arr[k-1];
}
void result( bool arr[], int n)
{
for ( int k=1; k<=n; k++)
cout << arr[k] << " " ;
}
int main()
{
int n = 5, m = 3;
bool arr[n+2] = {0};
command(arr, 1, 5);
command(arr, 2, 5);
command(arr, 3, 5);
process(arr, n);
result(arr, n);
return 0;
}
|
Java
class GFG
{
static void command( boolean arr[],
int a, int b)
{
arr[a] ^= true ;
arr[b + 1 ] ^= true ;
}
static void process( boolean arr[], int n)
{
for ( int k = 1 ; k <= n; k++)
{
arr[k] ^= arr[k - 1 ];
}
}
static void result( boolean arr[], int n)
{
for ( int k = 1 ; k <= n; k++)
{
if (arr[k] == true )
System.out.print( "1" + " " );
else
System.out.print( "0" + " " );
}
}
public static void main(String args[])
{
int n = 5 , m = 3 ;
boolean arr[] = new boolean [n + 2 ];
command(arr, 1 , 5 );
command(arr, 2 , 5 );
command(arr, 3 , 5 );
process(arr, n);
result(arr, n);
}
}
|
Python3
def command(brr, a, b):
arr[a] ^ = 1
arr[b + 1 ] ^ = 1
def process(arr, n):
for k in range ( 1 , n + 1 , 1 ):
arr[k] ^ = arr[k - 1 ]
def result(arr, n):
for k in range ( 1 , n + 1 , 1 ):
print (arr[k], end = " " )
if __name__ = = '__main__' :
n = 5
m = 3
arr = [ 0 for i in range (n + 2 )]
command(arr, 1 , 5 )
command(arr, 2 , 5 )
command(arr, 3 , 5 )
process(arr, n)
result(arr, n)
|
C#
using System;
class GFG
{
static void command( bool [] arr,
int a, int b)
{
arr[a] ^= true ;
arr[b + 1] ^= true ;
}
static void process( bool [] arr, int n)
{
for ( int k = 1; k <= n; k++)
{
arr[k] ^= arr[k - 1];
}
}
static void result( bool [] arr, int n)
{
for ( int k = 1; k <= n; k++)
{
if (arr[k] == true )
Console.Write( "1" + " " );
else
Console.Write( "0" + " " );
}
}
public static void Main()
{
int n = 5, m = 3;
bool [] arr = new bool [n + 2];
command(arr, 1, 5);
command(arr, 2, 5);
command(arr, 3, 5);
process(arr, n);
result(arr, n);
}
}
|
PHP
<?php
function command( $arr , $a , $b )
{
$arr [ $a ] = $arr [ $a ] ^ 1;
$arr [ $b + 1] ^= 1;
}
function process( $arr , $n )
{
for ( $k = 1; $k <= $n ; $k ++)
{
$arr [ $k ] = $arr [ $k ] ^ $arr [ $k - 1];
}
}
function result( $arr , $n )
{
for ( $k = 1; $k <= $n ; $k ++)
echo $arr [ $k ] . " " ;
}
$n = 5; $m = 3;
$arr = new SplFixedArray(7);
$arr [6] = array (0);
command( $arr , 1, 5);
command( $arr , 2, 5);
command( $arr , 3, 5);
process( $arr , $n );
result( $arr , $n );
?>
|
Javascript
<script>
function command(arr, a, b)
{
arr[a] ^= 1;
arr[b+1] ^= 1;
}
function process( arr, n)
{
for ( var k=1; k<=n; k++)
arr[k] ^= arr[k-1];
}
function result( arr, n)
{
for ( var k=1; k<=n; k++)
document.write( arr[k] + " " );
}
var n = 5, m = 3;
var arr = Array(n+2).fill(0);
command(arr, 1, 5);
command(arr, 2, 5);
command(arr, 3, 5);
process(arr, n);
result(arr, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...