Open In App

Range Queries for finding the Sum of all even parity numbers

Last Updated : 25 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 
 

Examples: 

Input: Q = [ [1, 10], [121, 211] ] 
Output: 
33 
7493 
Explanation: 
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]] 
Output: 
10 
137676 
14595 
250750  

 

Approach: 
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. 
 

  1. Initialise the prefix array pref[].
  2. 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[].
  3. 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 

C++




// C++ program to find the sum
// of all Even Parity numbers
// in the given range
 
#include <bits/stdc++.h>
using namespace std;
 
// pref[] array to precompute
// the sum of all Even
// Parity Numbers
int pref[100001] = { 0 };
 
// Function that returns true
// if count of set bits in
// x is even
int isEvenParity(int num)
{
    // Parity will store the
    // count of set bits
    int parity = 0;
    int x = num;
    while (x != 0) {
        if (x & 1)
            parity++;
        x = x >> 1;
    }
 
    if (parity % 2 == 0)
        return num;
    else
        return 0;
}
 
// Function to precompute the
// sum of all even parity
// numbers upto 100000
void preCompute()
{
    for (int i = 1; i < 100001; i++) {
 
        // isEvenParity()
        // return the number i
        // if i has even parity
        // else return 0
        pref[i] = pref[i - 1]
                  + isEvenParity(i);
    }
}
 
// Function to print sum
// for each query
void printSum(int L, int R)
{
    cout << (pref[R] - pref[L - 1])
         << endl;
}
 
// Function to print sum of all
// even parity numbers between
// [L, R]
void printSum(int arr[2][2], int Q)
{
 
    // Function that pre computes
    // the sum of all even parity
    // numbers
    preCompute();
 
    // Iterate over all Queries
    // to print sum
    for (int i = 0; i < Q; i++) {
        printSum(arr[i][0],
                 arr[i][1]);
    }
}
// Driver code
int main()
{
    // Queries
    int N = 2;
    int Q[2][2] = { { 1, 10 },
                    { 121, 211 } };
 
    // Function that print
    // the sum of all even parity
    // numbers in Range [L, R]
    printSum(Q, N);
 
    return 0;
}


Java




// Java program to find the sum
// of all Even Parity numbers
// in the given range
import java.io.*;
import java.util.*;
 
class GFG {
     
// pref[] array to precompute
// the sum of all Even
// Parity Numbers
static int[] pref = new int[100001];
 
// Function that returns true
// if count of set bits in
// x is even
static int isEvenParity(int num)
{
     
    // Parity will store the
    // count of set bits
    int parity = 0;
    int x = num;
     
    while (x != 0)
    {
        if ((x & 1) == 1)
            parity++;
             
        x = x >> 1;
    }
     
    if (parity % 2 == 0)
        return num;
    else
        return 0;
}
 
// Function to precompute the
// sum of all even parity
// numbers upto 100000
static void preCompute()
{
    for(int i = 1; i < 100001; i++)
    {
 
       // isEvenParity()
       // return the number i
       // if i has even parity
       // else return 0
       pref[i] = pref[i - 1] + isEvenParity(i);
    }
}
 
// Function to print sum
// for each query
static void printSum(int L, int R)
{
    System.out.println(pref[R] - pref[L - 1]);
}
 
// Function to print sum of all
// even parity numbers between
// [L, R]
static void printSum(int arr[][], int Q)
{
     
    // Function that pre computes
    // the sum of all even parity
    // numbers
    preCompute();
 
    // Iterate over all Queries
    // to print sum
    for(int i = 0; i < Q; i++)
    {
       printSum(arr[i][0], arr[i][1]);
    }
}
     
// Driver code
public static void main(String[] args)
{
     
    // Queries
    int N = 2;
    int[][] Q = { { 1, 10 },
                  { 121, 211 } };
 
    // Function that print
    // the sum of all even parity
    // numbers in Range [L, R]
    printSum(Q, N);
}
}
 
// This code is contributed by coder001


Python3




class GFG :
   
    # pref[] array to precompute
    # the sum of all Even
    # Parity Numbers
    pref = [0] * (100001)
     
    # Function that returns true
    # if count of set bits in
    # x is even
    @staticmethod
    def  isEvenParity( num) :
       
        # Parity will store the
        # count of set bits
        parity = 0
        x = num
        while (x != 0) :
            if ((x & 1) == 1) :
                parity += 1
            x = x >> 1
        if (parity % 2 == 0) :
            return num
        else :
            return 0
           
    # Function to precompute the
    # sum of all even parity
    # numbers upto 100000
    @staticmethod
    def preCompute() :
        i = 1
        while (i < 100001) :
           
            # isEvenParity()
            # return the number i
            # if i has even parity
            # else return 0
            GFG.pref[i] = GFG.pref[i - 1] + GFG.isEvenParity(i)
            i += 1
             
    # Function to print sum
    # for each query
    @staticmethod
    def printsum( L,  R) :
        print(GFG.pref[R] - GFG.pref[L - 1])
         
    # Function to print sum of all
    # even parity numbers between
    # [L, R]
    @staticmethod
    def printSum( arr,  Q) :
       
        # Function that pre computes
        # the sum of all even parity
        # numbers
        GFG.preCompute()
         
        # Iterate over all Queries
        # to print sum
        i = 0
        while (i < Q) :
            GFG.printsum(arr[i][0], arr[i][1])
            i += 1
             
    # Driver code
    @staticmethod
    def main( args) :
       
        # Queries
        N = 2
        Q = [[1, 10], [121, 211]]
         
        # Function that print
        # the sum of all even parity
        # numbers in Range [L, R]
        GFG.printSum(Q, N)
     
if __name__=="__main__":
    GFG.main([])
     
    # This code is contributed by aadityaburujwale.


C#




// C# program to find the sum
// of all Even Parity numbers
// in the given range
using System;
 
class GFG {
     
// pref[] array to precompute
// the sum of all Even
// Parity Numbers
static int[] pref = new int[100001];
 
// Function that returns true
// if count of set bits in
// x is even
static int isEvenParity(int num)
{
     
    // Parity will store the
    // count of set bits
    int parity = 0;
    int x = num;
     
    while (x != 0)
    {
        if ((x & 1) == 1)
            parity++;
             
        x = x >> 1;
    }
     
    if (parity % 2 == 0)
        return num;
    else
        return 0;
}
 
// Function to precompute the
// sum of all even parity
// numbers upto 100000
static void preCompute()
{
    for(int i = 1; i < 100001; i++)
    {
         
       // isEvenParity()
       // return the number i
       // if i has even parity
       // else return 0
       pref[i] = pref[i - 1] + isEvenParity(i);
    }
}
 
// Function to print sum
// for each query
static void printSum(int L, int R)
{
    Console.WriteLine(pref[R] - pref[L - 1]);
}
 
// Function to print sum of all
// even parity numbers between
// [L, R]
static void printSum(int[,] arr, int Q)
{
     
    // Function that pre computes
    // the sum of all even parity
    // numbers
    preCompute();
 
    // Iterate over all Queries
    // to print sum
    for(int i = 0; i < Q; i++)
    {
       printSum(arr[i, 0], arr[i, 1]);
    }
}
     
// Driver code
public static void Main()
{
     
    // Queries
    int N = 2;
    int[,] Q = { { 1, 10 },
                 { 121, 211 } };
 
    // Function that print
    // the sum of all even parity
    // numbers in Range [L, R]
    printSum(Q, N);
}
}
 
// This code is contributed by AbhiThakur


Javascript




<script>
// Javascript program to find the sum
// of all Even Parity numbers
// in the given range
 
// pref[] array to precompute
// the sum of all Even
// Parity Numbers
let pref = new Array(100001).fill(0);
 
// Function that returns true
// if count of set bits in
// x is even
function isEvenParity(num)
{
 
    // Parity will store the
    // count of set bits
    let parity = 0;
    let x = num;
    while (x != 0) {
        if (x & 1 == 1)
            parity++;
        x = x >> 1;
    }
 
    if (parity % 2 == 0)
        return num;
    else
        return 0;
}
 
// Function to precompute the
// sum of all even parity
// numbers upto 100000
function preCompute() {
    for (let i = 1; i < 100001; i++) {
 
        // isEvenParity()
        // return the number i
        // if i has even parity
        // else return 0
        pref[i] = pref[i - 1] + isEvenParity(i);
    }
}
 
// Function to print sum
// for each query
function printSum2(L, R) {
    document.write(pref[R] - pref[L - 1] + "<br>");
}
 
// Function to print sum of all
// even parity numbers between
// [L, R]
function printSum(arr, Q) {
 
    // Function that pre computes
    // the sum of all even parity
    // numbers
    preCompute();
 
    // Iterate over all Queries
    // to print sum
    for (let i = 0; i < Q; i++) {
        printSum2(arr[i][0], arr[i][1]);
    }
}
 
// Driver code
 
// Queries
let N = 2;
let Q = [[1, 10],
[121, 211]];
 
// Function that print
// the sum of all even parity
// numbers in Range [L, R]
printSum(Q, N);
 
// This code is contributed by _saurabh_jaiswal.
</script>


Output: 

33
7493

 

Time Complexity: O(Q*log(N)), where n is the size of the array and Q is the number of queries.
Auxiliary Space: O(1)



Similar Reads

Range Queries to count the number of even parity values with updates
Given an array arr[] of N integers, the task is to perform the following two queries: query(L, R): Print the number of Even Parity numbers in the subarray from L to R.update(i, x): Update the array element reference by index i to x. Examples: Input: arr[] = {18, 15, 8, 9, 14, 5} Query 1: query(L = 0, R = 4) Query 2: update(i = 3, x = 11) Query 3: q
25 min read
Parity of count of letters whose position and frequency have same parity
Given a string S of lowercase English characters, find out whether the number of the letters whose frequencies in the string have the same parity as their positions in the English alphabet is odd or even (i.e., they have an odd frequency in the string and their position is at an odd number in the English alphabet or have even frequency and their po
8 min read
Array range queries over range queries
Given an array of size n and a give set of commands of size m. The commands are enumerated from 1 to m. These commands can be of the following two types of commands: Type 1 [l r (1 &lt;= l &lt;= r &lt;= n)] : Increase all elements of the array by one, whose indices belongs to the range [l, r]. In these queries of the index is inclusive in the range
41 min read
Finding the Parity of a number Efficiently
Given an integer N. The task is to write a program to find the parity of the given number. Note: Parity of a number is used to define if the total number of set-bits(1-bit in binary representation) in a number is even or odd. If the total number of set-bits in the binary representation of a number is even then the number is said to have even parity
8 min read
Remove all even parity nodes from a Doubly and Circular Singly Linked List
Given a Doubly linked list and Circular singly linked list containing N nodes, the task is to remove all the nodes from each list which contains elements whose parity is even. Example: Input: CLL = 9 -&gt; 11 -&gt; 34 -&gt; 6 -&gt; 13 -&gt; 21 Output: 11 -&gt; 13 -&gt; 21 Explanation: The circular singly linked list contains : 11 -&gt; 1011, parity
27 min read
Sum of elements from an array having even parity
Given an array arr[], the task is to calculate the sum of the elements from the given array which has even parity i.e. the number of set bits is even using bitwise operator. Examples: Input: arr[] = {2, 4, 3, 5, 9} Output: 17 Only 3(0011), 5(0101) and 9(1001) have even parity So 3 + 5 + 9 = 17 Input: arr[] = {1, 5, 4, 16, 10} Output: 15 Approach: I
6 min read
Find the maximum sum pair in an Array with even parity
Given an array arr[] of N integers, the task is to find a pair with even parity and maximum sum. Examples: Input: arr[] = {18, 15, 8, 9, 14} Output: 18 15 Explanation: Binary Representation of elements - 18 =&gt; 10010, Parity = 2 15 =&gt; 1111, Parity = 4 8 =&gt; 1000, Parity = 1 9 =&gt; 1001, Parity = 2 14 =&gt; 1110, Parity = 3 Here, parity for
7 min read
Queries to check whether all the elements in the given range occurs even number of times
Given an array arr[] containing N integers and there are Q queries where each query consists of a range [L, R]. The task is to find whether all the elements from the given index range have even frequency or not.Examples: Input: arr[] = {1, 1, 2, 2, 1}, Q[][] = {{1, 5}, {1, 4}, {3, 4}} Output: No Yes YesInput: arr[] = {100, 100, 200, 100}, Q[][] = {
6 min read
Sum of all palindromic numbers lying in the range [L, R] for Q queries
Given Q queries in the form of 2D array arr[][] whose every row consists of two numbers L and R which denotes the range [L, R], the task is to find the sum of all Palindrome Numbers lying in range [L, R]. Input: Q = 2, arr[][] = { {10, 13}, {12, 21} } Output: 11 0 Explanation: From 10 to 13 only 11 is the palindrome number From 12 to 21 there is no
8 min read
Sum of all armstrong numbers lying in the range [L, R] for Q queries
Given Q queries in the form of a 2D array arr[][] in which every row consists of two numbers L and R which denotes a range [L, R], the task is to find the sum of all Armstrong Numbers lying in the given range [L, R].Examples: Input: Q = 2, arr = [ [1, 13], [121, 211] ] Output: 45 153 Explanation: From 1 to 13, 1, 2, 3, 4, 5, 6, 7, 8 and 9 are the A
9 min read