Given Q queries where each query consists of two numbers L and R which denotes a range [L, R]. The task is to find the sum of all Even Parity Numbers lying in the given range [L, R].
Parity of a number refers to whether it contains an odd or even number of 1-bits. The number has Even Parity if it contains even number of 1-bits.
Input: Q = [ [1, 10], [121, 211] ]
binary(1) = 01, parity = 1
binary(2) = 10, parity = 1
binary(3) = 11, parity = 2
binary(4) = 100, parity = 1
binary(5) = 101, parity = 2
binary(6) = 110, parity = 2
binary(7) = 111, parity = 3
binary(8) = 1000, parity = 1
binary(9) = 1001, parity = 2
binary(10) = 1010, parity = 2
From 1 to 10, 3, 5, 6, 9 and 10 are the Even Parity numbers. Therefore the sum is 33.
From 121 to 211 the sum of all the even parity numbers is 7493.
Input: Q = [ [ 10, 10 ], [ 258, 785 ], [45, 245], [ 1, 1000]]
The idea is to use a Prefix Sum Array. The sum of all Even Parity Numbers till that particular index is precomputed and stored in an array pref so that every query can be answered in O(1) time.
- Initialise the prefix array pref.
- Iterate from 1 to N and check if the number has even parity or not:
- If the number is Even Parity Number then, the current index of pref will store the sum of Even Parity Numbers found so far.
- Else the current index of pref is same as the value at previous index of pref.
- For Q queries the sum of all Even Parity Numbers for range [L, R] can be calculated as follows:
sum = pref[R] - pref[L - 1]
Below is the implementation of the above approach
Time Complexity: O(N), where N is the maximum element in the query.
- Range Queries to count the number of even parity values with updates
- Sum of all Non-Fibonacci numbers in a range for Q queries
- Finding the Parity of a number Efficiently
- Sum of all composite numbers lying in the range [L, R] for Q queries
- Queries for GCD of all numbers of an array except elements in a given range
- Sum of all palindromic numbers lying in the range [L, R] for Q queries
- Sum of prime numbers in range [L, R] from given Array for Q queries
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Range Queries for count of Armstrong numbers in subarray using MO's algorithm
- Array Range Queries to count Powerful numbers with updates
- Range Queries for number of Armstrong numbers in an array with updates
- Queries for the difference between the count of composite and prime numbers in a given range
- Array range queries to count the number of Fibonacci numbers with updates
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- Array range queries over range queries
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Binary Indexed Tree : Range Update and Range Queries
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Range LCM Queries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.