Special Subarray
Practice
4.4 (16 votes)
Binary search
Algorithms
Problem
46% Success 2322 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given an array \(A\) of length \(N\). A subarray is called special if the XOR and Sum of the subarray are the same. Find the number of special subarrays of \(A\).
Input Format
- The first line contains an integer \(T\), which denotes the number of test cases.
- The first line of each test case contains an integer \(N\), which denotes the length of array \(A\).
- The second line of each test case contains \(N\) space-separated integers, denoting elements of array \(A\).
Output Format
For each test case, print the number of special subarrays of \(A\).
Constraints
\(1 \leq T \leq 10\)\(1 \leq N \leq 10^5\)\(1 \leq A_i \leq 10^9\)
Sample Input
2 3 2 1 6 2 4 5
Sample Output
5 2
Explanation
For test case \(1\):
- Subarray \([2]\), sum=2, XOR=2.
- Subarray \([2, 1]\), sum=3, XOR=3.
- Subarray \([2, 1, 6]\), sum=9, XOR=5.
- Subarray \([1]\), sum=1, XOR=1.
- Subarray \([1, 6]\), sum=7, XOR=7.
- Subarray \([6]\), sum=6, XOR=6.
Hence, the answer is \(5\).
For test case \(2\):
- Subarray \([4]\), sum=4, XOR=4.
- Subarray \([4, 5]\), sum=9, XOR=1.
- Subarray \([5]\), sum=5, XOR=5.
Hence, the answer is \(2\).
Code Editor
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Submissions
Please login to view your submissions
Similar Problems
Points:30
13 votes
Tags:
AlgorithmsApprovedBinary SearchDynamic ProgrammingMediumOpenSorting
Points:30
88 votes
Tags:
ApprovedBinary SearchMediumOpenSorting
Points:20
264 votes
Tags:
Ad-HocHiringReadySieveApprovedEasy
Editorial
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor