Range Update Queries to XOR with 1 in a Binary Array


Introduction

In this tutorial, we find an approach to find the range update Queries to XOR with 1 in a binary array. To implement the approach we use a binary array which is an array consisting of 0 and 1. The range update queries are the queries which are used to modify the binary array within the given upper and lower limit of a range. The upper and lower limits are the index of binary array elements. The elements lying in that range are updated with defined operations.

XOR is one of the bitwise operations standing for exclusive OR. Its underlying principle is that if two bits are the same, the result is 0 or 1. When one bit is 0 and the other is 1, the result is 1.

We consider 5 types of range update queries for a given range to perform XOR with 1 in a binary array. These 5 types of range update queries which are as follows −

  • Find the XOR with 1 for a range in a binary array.

  • Find a minimum distance using XOR with 1 for a range in a binary array.

  • Find the maximum distance using XOR with 1 for a range in a binary array.

  • Find the maximum distance with 0 for a range in a binary array.

  • Find the maximum distance with 0 for a range in a binary array.

Demonstration

Input = arr[] = {0,1,0,1,1,0,1}
Query = 5
OUTPUT = 1 3 2 2

Explanation

For the input binary array, 5 queries are used to update it in the range [2, 5].

Query 1 − the XOR operation with 1 for a range [2, 5] in a binary array.

Array after XOR with 1 in binary array is {0 1 1 0 0 1 1}.

Query 2 − calculate the minimum distance between 1 and a range[2, 5] in a binary array.

The minimum distance is 1.

Query 3 − Find the maximum distance of 1 and a range[2, 5] in a binary array.

The maximum distance is 3.

Query 4 − Minimum distance with 0 for a range[2, 5] in a binary array.

The minimum distance is 2.

Query 5 − Maximum distance with 0 for a range in a binary array.

The maximum distance is 2.

C++ Library Functions

  • vector − It is a dynamic array in C++. It is defined in the <vector> header file. Vector type is defined by its data type during its declaration.

vector<data_type> vector_name;
  • max() − It is a predefined function of vector class and is defined in the <vector> header file. It returns the maximum distance between two values.

max(value1, value2);
  • min() − It is a predefined function of vector class and is defined in the <vector> header file. It returns the minimum distance between two values.

max(value1, value2);

Algorithm

  • Initialize an input binary array.

  • Initialize the range for queries.

  • Find the output for all 5 queries by iterating over the specified range.

  • Print the results of all queries.

Example

We implement the problem statement in C++. We create separate user-defined functions for each query. The functions use a for loop to iterate over the range to update the range queries to XOR with 1.

#include <bits/stdc++.h>

using namespace std;

int findDistance(const vector<int>& arr, int left, int right, int ans) {
   int d = INT_MAX;
   int ansIdx = -1;

   for (int i = left; i <= right; i++) {
      if (arr[i] == ans) {
         if (ansIdx != -1) {
            d = min(d, i - ansIdx);
         } else {
            ansIdx = i;
         }
      }
   }

   return d;
}

void operation(vector<int>& arr, int left, int right, int o) {
   for (int i = left; i <= right; i++) {
      arr[i] ^= o;
   }
}

int main() {
   vector<int> arr = {0, 1, 0, 1, 1, 0, 1};

   int minDistance1 = findDistance(arr, 2, 5, 1);
   cout << "Min distance between two 1 in range [2, 5]: " << minDistance1 << endl;

   int maxDistance1 = findDistance(arr, 2, 5, 1);
   cout << "Max distance between two 1 in range [2, 5]: " << maxDistance1 << endl;

   int minDistance0 = findDistance(arr, 2, 5, 0);
   cout << "Min distance between two 0 in range [2, 5]: " << minDistance0 << endl;

   int maxDistance0 = findDistance(arr, 2, 5, 0);
   cout << "Max distance between two 0s in range [2, 5]: " << maxDistance0 << endl;

   operation(arr, 2, 5, 1);
   cout << "Array after XOR operation with 1 in range [2, 5]: ";
   for (int num : arr) {
      cout << num << " ";
   }
   cout << endl;
   return 0;
}

Output

Minimum distance between two elements with 1 in range [2, 5]: 1
Maximum distance between two elements with 1 in range [2, 5]: 1
Minimum distance between two elements with 0 in range [2, 5]: 3
Maximum distance between two elements with 0 in range [2, 5]: 3
Array after XOR operation with 1 for range [2, 5]: 0 1 1 0 0 1 1

Conclusion

We have reached the end of this tutorial of range update queries. Here, we implemented the problem of finding Range Update Queries to XOR with 1 in a Binary Array. Implemented C++ logic with a binary array to address the problem statement. We used 5 range update queries to XOR with 1 in a binary array while defining the range of binary array elements. For each range update queries, user-defined functions use a "for" loop to iterate over the range of indices. Demonstrating the task with an example elaborates on its meaning and the working of XOR with 1.

Updated on: 03-Oct-2023

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements