
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to find number of l-r pairs for which XORed results same as summation
Suppose we have an array A with N elements. We have to find the number of pairs of integers l and r that satisfies A[l] XOR A[l+1] XOR ... XOR A[r-1] XOR A[r] = A[l] + A[l+1] + ... A[r].
So, if the input is like A = [2, 5, 4, 6], then the output will be 5, because for pairs (1,1), (2,2), (3,3), (4,4) and (1,2).
Steps
To solve this, we will follow these steps −
n := size of A Define some arrays of size (n + 1) each, a, s and sx for initialize i := 1, when i <= n, update (increase i by 1), do: a[i] := A[i - 1] s[i] := s[i - 1] + a[i] sx[i] := sx[i - 1] XOR a[i] res := 0 for initialize l := 1, when l <= n, update (increase l by 1), do: bg := l, en = n, r = l while bg <= en, do: mi := (bg + en) / 2 if s[mi] - s[l - 1] is same as (sx[mi] XOR sx[l - 1]), then: r := mi bg := mi + 1 Otherwise en := mi - 1 res := res + (r - l + 1) return res
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> A){ int n = A.size(); vector<int> a(n + 1), s(n + 1), sx(n + 1); for (int i = 1; i <= n; i++){ a[i] = A[i - 1]; s[i] = s[i - 1] + a[i]; sx[i] = sx[i - 1] ^ a[i]; } int res = 0; for (int l = 1; l <= n; l++){ int bg = l, en = n, r = l; while (bg <= en){ int mi = (bg + en) / 2; if (s[mi] - s[l - 1] == (sx[mi] ^ sx[l - 1])){ r = mi; bg = mi + 1; } else en = mi - 1; } res += (r - l + 1); } return res; } int main(){ vector<int> A = { 2, 5, 4, 6 }; cout << solve(A) << endl; }
Input
{ 2, 5, 4, 6 }
Output
5
- Related Articles
- Program to find number of quadruples for which product of first and last pairs are same in Python
- C++ program to find maximum possible value for which XORed sum is maximum
- Program to count index pairs for which array elements are same in Python
- Find number of magical pairs of string of length L in C++.
- Program to find number of unique subsequences same as target in C++
- Program to find decode XORed permutation in Python
- Python program to find list of triplets for which i+j+k is not same as n
- C++ program to find permutation for which sum of adjacent elements sort is same as given array
- C++ program to find maximum possible value of XORed sum
- Program to find number of good pairs in Python
- Program to find smallest index for which array element is also same as index in Python
- Program to find higher number with same number of set bits as n in Python?\n
- Program to find number of pairs (i, j) such that ith and jth elements are same in Python
- Maximize the summation of numbers in a maximum of K moves in range [L, R] in C++
- Program to find minimum number of subsequence whose concatenation is same as target in python

Advertisements