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++
// CPP program to find modified array after
// m range toggle operations.
#include<bits/stdc++.h>
using namespace std;
// function for toggle
void command(bool arr[], int a, int b)
{
arr[a] ^= 1;
arr[b+1] ^= 1;
}
// function for final processing of array
void process(bool arr[], int n)
{
for (int k=1; k<=n; k++)
arr[k] ^= arr[k-1];
}
// function for printing result
void result(bool arr[], int n)
{
for (int k=1; k<=n; k++)
cout << arr[k] <<" ";
}
// driver program
int main()
{
int n = 5, m = 3;
bool arr[n+2] = {0};
// function call for toggle
command(arr, 1, 5);
command(arr, 2, 5);
command(arr, 3, 5);
// process array
process(arr, n);
// print result
result(arr, n);
return 0;
}
Java
// Java program to find modified array
// after m range toggle operations.
class GFG
{
// function for toggle
static void command(boolean arr[],
int a, int b)
{
arr[a] ^= true;
arr[b + 1] ^= true;
}
// function for final processing of array
static void process(boolean arr[], int n)
{
for (int k = 1; k <= n; k++)
{
arr[k] ^= arr[k - 1];
}
}
// function for printing result
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" + " ");
}
}
// Driver Code
public static void main(String args[])
{
int n = 5, m = 3;
boolean arr[] = new boolean[n + 2];
// function call for toggle
command(arr, 1, 5);
command(arr, 2, 5);
command(arr, 3, 5);
// process array
process(arr, n);
// print result
result(arr, n);
}
}
// This code is contributed
// by PrinciRaj1992
Python3
# Python 3 program to find modified array after
# m range toggle operations.
# function for toggle
def command(brr, a, b):
arr[a] ^= 1
arr[b+1] ^= 1
# function for final processing of array
def process(arr, n):
for k in range(1, n + 1, 1):
arr[k] ^= arr[k - 1]
# function for printing result
def result(arr, n):
for k in range(1, n + 1, 1):
print(arr[k], end = " ")
# Driver Code
if __name__ == '__main__':
n = 5
m = 3
arr = [0 for i in range(n+2)]
# function call for toggle
command(arr, 1, 5)
command(arr, 2, 5)
command(arr, 3, 5)
# process array
process(arr, n)
# print result
result(arr, n)
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to find modified array
// after m range toggle operations.
using System;
class GFG
{
// function for toggle
static void command(bool[] arr,
int a, int b)
{
arr[a] ^= true;
arr[b + 1] ^= true;
}
// function for final processing of array
static void process(bool[] arr, int n)
{
for (int k = 1; k <= n; k++)
{
arr[k] ^= arr[k - 1];
}
}
// function for printing result
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" + " ");
}
}
// Driver Code
public static void Main()
{
int n = 5, m = 3;
bool[] arr = new bool[n + 2];
// function call for toggle
command(arr, 1, 5);
command(arr, 2, 5);
command(arr, 3, 5);
// process array
process(arr, n);
// print result
result(arr, n);
}
}
// This code is contributed
// by Akanksha Rai
PHP
<?php
// PHP program to find modified array
// after m range toggle operations.
// function for toggle
function command($arr, $a, $b)
{
$arr[$a] = $arr[$a] ^ 1;
$arr[$b + 1] ^= 1;
}
// function for final processing
// of array
function process($arr, $n)
{
for ($k = 1; $k <= $n; $k++)
{
$arr[$k] = $arr[$k] ^ $arr[$k - 1];
}
}
// function for printing result
function result($arr, $n)
{
for ($k = 1; $k <= $n; $k++)
echo $arr[$k] . " ";
}
// Driver Code
$n = 5; $m = 3;
$arr = new SplFixedArray(7);
$arr[6] = array(0);
// function call for toggle
command($arr, 1, 5);
command($arr, 2, 5);
command($arr, 3, 5);
// process array
process($arr, $n);
// print result
result($arr, $n);
// This code is contributed
// by Mukul Singh
?>
JavaScript
<script>
// Javascript program to find modified array after
// m range toggle operations.
// function for toggle
function command(arr, a, b)
{
arr[a] ^= 1;
arr[b+1] ^= 1;
}
// function for final processing of array
function process( arr, n)
{
for (var k=1; k<=n; k++)
arr[k] ^= arr[k-1];
}
// function for printing result
function result( arr, n)
{
for (var k=1; k<=n; k++)
document.write( arr[k] + " ");
}
// driver program
var n = 5, m = 3;
var arr = Array(n+2).fill(0);
// function call for toggle
command(arr, 1, 5);
command(arr, 2, 5);
command(arr, 3, 5);
// process array
process(arr, n);
// print result
result(arr, n);
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Count and Toggle Queries on a Binary Array
Given a size n in which initially all elements are 0. The task is to perform multiple queries of following two types. The queries can appear in any order. 1. toggle(start, end) : Toggle (0 into 1 or 1 into 0) the values from range 'start' to 'end'. 2. count(start, end) : Count the number of 1's with
15+ min read
Range Update Queries to XOR with 1 in a Binary Array.
Given a binary array arr[] of size N. The task is to answer Q queries which can be of any one type from below: Type 1 - 1 l r : Performs bitwise xor operation on all the array elements from l to r with 1. Type 2 - 2 l r : Returns the minimum distance between two elements with value 1 in a subarray [
15+ min read
Gray to Binary and Binary to Gray conversion
Binary Number is the default way to store numbers, but in many applications, binary numbers are difficult to use and a variety of binary numbers is needed. This is where Gray codes are very useful. Gray code has a property that two successive numbers differ in only one bit because of this property g
10 min read
Toggle all bits after most significant bit
Given a number, toggle all bits of it after most significant bit including most significant bit. Examples : Input : 10 Output : 5 Binary representation of 10 is 1010 After toggling we get 0101 Input : 5 Output : 2 We can toggle a bit by doing XOR of it with 1 (Note that 1 ^ 0 = 1 and 1 ^ 1 = 0). The
11 min read
Minimum toggles to partition a binary array so that it has first 0s then 1s
Given an array of n integers containing only 0 and 1. Find the minimum toggles (switch from 0 to 1 or vice-versa) required such the array become partitioned, i.e., it has first 0s than 1s. There should be at least one 0 in the beginning, and there can be zero or more 1s in the end. Input: arr[] = {1
7 min read
Toggle case of a string using Bitwise Operators
Given a string, write a function that returns toggle case of a string using the bitwise operators in place.In ASCII codes, character âAâ is integer 65 = (0100 0001)2, while character âaâ is integer 97 = (0110 0001)2. Similarly, character 'D' is integer 68 = (0100 0100)2, while character 'd' is integ
4 min read
Find Binary string by converting all 01 or 10 to 11 after M iterations
Given a binary string str[] of size N and an integer M. This binary string can be modified by flipping all the 0's to 1 which have exactly one 1 as a neighbour. The task is to find the final state of the binary string after M such iterations.Note: 2?N?103, 1?M?109 Examples: Input: str="01100", M=1Ou
8 min read
Toggle bits in the given range
Given a non-negative number n and two values l and r. The problem is to toggle the bits in the range l to r in the binary representation of n, i.e., to toggle bits from the lth least significant bit bit to the rth least significant bit (the rightmost bit as counted as first bit). A toggle operation
9 min read
Convert all numbers in range [L, R] to binary number
Given two positive integer numbers L and R. The task is to convert all the numbers from L to R to binary number. Examples: Input: L = 1, R = 4Output: 11011100Explanation: The binary representation of the numbers 1, 2, 3 and 4 are: 1 = (1)22 = (10)23 = (11)24 = (100)2 Input: L = 2, R = 8Output:101110
5 min read
Array range queries over range queries
Given an array of size n and a give set of commands of size m. The commands are enumerated from 1 to m. These commands can be of the following two types of commands: Type 1 [l r (1 <= l <= r <= n)] : Increase all elements of the array by one, whose indices belongs to the range [l, r]. In th
15+ min read