BitwiseBafflement
Practice
4.2 (6 votes)
Bit manipulation
Basics of bit manipulation
Number theory
Math
Algorithms
Basic programming
Problem
69% Success 751 Attempts 50 Points 3s Time Limit 256MB Memory 1024 KB Max Code

You are given an array \(A\) of size \(N\). Your task is to find the number of subarrays within the given array such that:

  • The Bitwise AND of all elements in the subarray has an odd number of set bits (i.e., 1s), and
  • The Bitwise XOR of all elements in the subarray has an even number of set bits (i.e., 1s)

Note: The subarray of \(A\) is a contiguous part of the array \(A\), i. e. the array \(A_i, A_{i + 1}, ...., A_j\) for some \(1 \leq i \leq j \leq N\).

Input format

  • The first line contains a single integer \(T\), which denotes the number of test cases.
  • For each test case:
    • The first line contains \(N\) denoting the size of array \(A\).
    • The second line contains \(N\) space-separated integers, denoting the elements of \(A\).

Output format

For each test case, print the number of subarrays within the given array such that the Bitwise AND of all elements in the subarray has an odd number of set bits (i.e., 1s) and Bitwise XOR of all elements in the subarray has an even number of set bits (i.e., 1s) in a new line.

Constraints

\(1 \leq T \leq 10^5 \\ 1 \leq N \leq 10^6 \\ 1≤A[i]≤10^9\ ∀\ i∈[1,N] \\ \text{The sum of all values of N over all test cases doesn't exceed } 10^6\)

 

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

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
187 votes
Tags:
Ad-HocAlgorithmsApprovedDynamic ProgrammingMediumOpen
Points:50
30 votes
Tags:
AlgorithmsApprovedBit manipulationHardOpenSorting