Skip to content
Related Articles

Related Articles

Queries on probability of even or odd number in given ranges

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 20 Jul, 2022
Improve Article
Save Article

Given an array A of size N, containing integers. We have to answer Q queries where each query is of the form: 

  • K L R : If K = 0, then you have to find the probability of choosing an even number from the segment [L, R] (both inclusive) in the array A. 
  • K L R : If K = 1, then you have to find the probability of choosing an odd number from the segment [L, R] (both inclusive) in the array A. 

For each query print two integers p and q which represent the probability p/q. Both p and q are reduced to the minimal form. 
If p is 0 print 0 or if p is equal to q print 1, otherwise print p and q alone. 

Examples: 

Input : N = 5, arr[] = { 6, 5, 2, 1, 7 }
        query 1: 0 2 2
        query 2: 1 2 5 
        query 3: 0 1 4  
Output : 0
         3 4
         1 2
Explanation : 
First query is to find probability of even 
element in range [2, 2]. Since range contains 
a single element 5 which is odd, the answer 
is 0. Second query is to find probability of
odd element in range [2, 5]. There are 3
odd elements in range probability is 3/4.
Third query is for even elements in range
from 1 to 4. Since there are equal even
and odd elements, probability is 2/4
which is 1/2.

The idea is to maintain two arrays, say even[] and odd[], which maintain the number of even or odd element upto index i. Now, to answer each query, we can compute result denominator q by finding number of element in the given query range. To find result numerator, we remove number of elements upto l – 1 from elements upto r. 
To output the answer in minimal form, we find the GCD of p and q and output p/gcd and q/gcd. For answer 0 and 1, we will explicitly specify the conditions.

Below is the implementation of this approach: 

C++




// CPP program to find probability of even
// or odd elements in a given range.
#include <bits/stdc++.h>
using namespace std;
 
// Number of tuples in a query
#define C 3
 
// Solve each query of K L R form
void solveQuery(int arr[], int n, int Q,
                           int query[][C])
{
    // To count number of odd and even
    // number upto i-th index.
    int even[n + 1];
    int odd[n + 1];
    even[0] = odd[0] = 0;
 
    // Counting number of odd and even
    // integer upto index i
    for (int i = 0; i < n; i++) {
 
        // If number is odd, increment the
        // count of odd frequency leave
        // even frequency same.
        if (arr[i] & 1) {
            odd[i + 1] = odd[i] + 1;
            even[i + 1] = even[i];
        }
 
        // If number is even, increment the
        // count of even frequency leave odd
        // frequency same.
        else {
            even[i + 1] = even[i] + 1;
            odd[i + 1] = odd[i];
        }
    }
 
    // To solve each query
    for (int i = 0; i < Q; i++) {
        int r = query[i][2];
        int l = query[i][1];
        int k = query[i][0];
 
        // Counting total number of element in
        // current query
        int q = r - l + 1;
        int p;
 
        // Counting number of odd or even element
        // in current query range
        if (k)
            p = odd[r] - odd[l - 1];
        else
            p = even[r] - even[l - 1];
 
        // If frequency is 0, output 0
        if (!p)
            cout << "0" << endl;
 
        // If frequency is equal to number of 
        // element in current range output 1.
        else if (p == q)
            cout << "1" << endl;
 
        // Else find the GCD of both. If yes,
        // output by dividing both number by gcd
        // to output the answer in reduced form.
        else {
            int g = __gcd(p, q);
            cout << p / g << " " << q / g << endl;
        }
    }
}
// Driven Program
int main()
{
    int arr[] = { 6, 5, 2, 1, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int Q = 2;
    int query[Q][C] = {
        { 0, 2, 2 },
        { 1, 2, 5 }
    };
 
    solveQuery(arr, n, Q, query);
    return 0;
}

Java




// java program to find probability
// of even or odd elements in a
// given range.
import java.io.*;
 
public class GFG {
         
    // Number of tuples in a query
    //static int C = 3;
    // Recursive function to return
    // gcd of a and b
    static int __gcd(int a, int b)
    {
        // Everything divides 0
        if (a == 0 || b == 0)
            return 0;
     
        // base case
        if (a == b)
            return a;
     
        // a is greater
        if (a > b)
            return __gcd(a - b, b);
             
        return __gcd(a, b - a);
    }
     
    // Solve each query of K L R form
    static void solveQuery(int []arr,
             int n, int Q, int [][]query)
    {
         
        // To count number of odd and even
        // number upto i-th index.
        int []even = new int[n + 1];
        int []odd = new int[n + 1];
        even[0] = odd[0] = 0;
     
        // Counting number of odd and even
        // integer upto index i
        for (int i = 0; i < n; i++)
        {
     
            // If number is odd, increment
            // the count of odd frequency
            // leave even frequency same.
            if ((arr[i] & 1) > 0)
            {
                odd[i + 1] = odd[i] + 1;
                even[i + 1] = even[i];
            }
     
            // If number is even, increment
            // the count of even frequency
            // leave odd frequency same.
            else
            {
                even[i + 1] = even[i] + 1;
                odd[i + 1] = odd[i];
            }
        }
     
        // To solve each query
        for (int i = 0; i < Q; i++)
        {
            int r = query[i][2];
            int l = query[i][1];
            int k = query[i][0];
     
            // Counting total number of
            // element in current query
            int q = r - l + 1;
            int p;
     
            // Counting number of odd or
            // even element in current
            // query range
            if (k > 0)
                p = odd[r] - odd[l - 1];
            else
                p = even[r] - even[l - 1];
     
            // If frequency is 0, output 0
            if (p <= 0)
                System.out.println("0");
     
            // If frequency is equal to
            // number of element in current
            // range output 1.
            else if (p == q)
                System.out.println("1");
     
            // Else find the GCD of both.
            // If yes, output by dividing
            // both number by gcd to output
            // the answer in reduced form.
            else
            {
                int g = __gcd(p, q);
                System.out.println(p / g
                          + " " + q / g);
            }
        }
    }
     
    // Driven Program
    static public void main (String[] args)
    {
        int []arr = { 6, 5, 2, 1, 7 };
        int n = arr.length;
        int Q = 2;
        int [][]query = { { 0, 2, 2 },
                          { 1, 2, 5 } };
 
        solveQuery(arr, n, Q, query);
    }
}
 
// This code is contributed by vt_m.

Python 3




# Python 3 program to find probability
# of even or odd elements in a given range.
import math
 
# Number of tuples in a query
C = 3
 
# Solve each query of K L R form
def solveQuery(arr, n, Q, query):
 
    # To count number of odd and even
    # number upto i-th index.
    even = [0] * (n + 1)
    odd = [0] * (n + 1)
    even[0] = 0
    odd[0] = 0
 
    # Counting number of odd and even
    # integer upto index i
    for i in range( n) :
 
        # If number is odd, increment the
        # count of odd frequency leave
        # even frequency same.
        if (arr[i] & 1) :
            odd[i + 1] = odd[i] + 1
            even[i + 1] = even[i]
 
        # If number is even, increment the
        # count of even frequency leave odd
        # frequency same.
        else :
            even[i + 1] = even[i] + 1
            odd[i + 1] = odd[i]
 
    # To solve each query
    for i in range( Q) :
        r = query[i][2]
        l = query[i][1]
        k = query[i][0]
 
        # Counting total number of element 
        # in current query
        q = r - l + 1
 
        # Counting number of odd or even 
        # element in current query range
        if (k):
            p = odd[r] - odd[l - 1]
        else:
            p = even[r] - even[l - 1]
 
        # If frequency is 0, output 0
        if (not p):
            print("0")
 
        # If frequency is equal to number of
        # element in current range output 1.
        elif (p == q):
            print("1")
 
        # Else find the GCD of both. If yes,
        # output by dividing both number by gcd
        # to output the answer in reduced form.
        else :
            g = math.gcd(p, q)
            print((p // g), (q // g))
 
# Driver Code
if __name__ =="__main__":
     
    arr = [ 6, 5, 2, 1, 7 ]
    n = len(arr)
    Q = 2
    query = [[0, 2, 2],
             [1, 2, 5]]
 
    solveQuery(arr, n, Q, query)
 
# This code is contributed by ita_c

C#




// C# program to find probability
// of even or odd elements in a
// given range.
using System;
 
public class GFG {
     
    // Number of tuples in a query
    //static int C = 3;
    // Recursive function to return
    // gcd of a and b
    static int __gcd(int a, int b)
    {
         
        // Everything divides 0
        if (a == 0 || b == 0)
            return 0;
     
        // base case
        if (a == b)
            return a;
     
        // a is greater
        if (a > b)
            return __gcd(a - b, b);
             
        return __gcd(a, b - a);
    }
     
    // Solve each query of K L R form
    static void solveQuery(int []arr,
           int n, int Q, int [,]query)
    {
         
        // To count number of odd and
        // even number upto i-th index.
        int []even = new int[n + 1];
        int []odd = new int[n + 1];
        even[0] = odd[0] = 0;
     
        // Counting number of odd and
        // even integer upto index i
        for (int i = 0; i < n; i++)
        {
     
            // If number is odd,
            // increment the count of
            // odd frequency leave
            // even frequency same.
            if ((arr[i] & 1) > 0)
            {
                odd[i + 1] = odd[i] + 1;
                even[i + 1] = even[i];
            }
     
            // If number is even,
            // increment the count of
            // even frequency leave
            // odd frequency same.
            else
            {
                even[i + 1] = even[i] + 1;
                odd[i + 1] = odd[i];
            }
        }
     
        // To solve each query
        for (int i = 0; i < Q; i++)
        {
            int r = query[i,2];
            int l = query[i,1];
            int k = query[i,0];
     
            // Counting total number of
            // element in current query
            int q = r - l + 1;
            int p;
     
            // Counting number of odd
            // or even element in current
            // query range
            if (k > 0)
                p = odd[r] - odd[l - 1];
            else
                p = even[r] - even[l - 1];
     
            // If frequency is 0, output 0
            if (p <= 0)
                Console.WriteLine("0");
     
            // If frequency is equal to
            // number of element in
            // current range output 1.
            else if (p == q)
                Console.WriteLine("1");
     
            // Else find the GCD of both.
            // If yes, output by dividing
            // both number by gcd to output
            // the answer in reduced form.
            else
            {
                int g = __gcd(p, q);
                Console.WriteLine(p / g
                           + " " + q / g);
            }
        }
    }
     
    // Driven Program
    static public void Main ()
    {
        int []arr = { 6, 5, 2, 1, 7 };
        int n = arr.Length;
        int Q = 2;
        int [,]query = { { 0, 2, 2 },
                         { 1, 2, 5 } };
     
        solveQuery(arr, n, Q, query);
    }
}
 
// This code is contributed by vt_m.

PHP




<?php
// PHP program to find probability
// of even or odd elements in a
// given range.
 
// Number of tuples in a query
//static int C = 3;
// Recursive function to return
// gcd of a and b
function __gcd($a, $b)
{
    // Everything divides 0
    if ($a == 0 || $b == 0)
        return 0;
 
    // base case
    if ($a == $b)
        return $a;
 
    // a is greater
    if ($a > $b)
        return __gcd($a - $b, $b);
         
    return __gcd($a, $b - $a);
}
 
// Solve each query of K L R form
function solveQuery($arr, $n, $Q, $query)
{
     
    // To count number of odd and even
    // number upto i-th index.
    // $even = new int[n + 1];
    // int []odd = new int[n + 1];
    $even[0] = $odd[0] = 0;
 
    // Counting number of odd and even
    // integer upto index i
    for ($i = 0; $i < $n; $i++)
    {
 
        // If number is odd, increment
        // the count of odd frequency
        // leave even frequency same.
        if (($arr[$i] & 1) > 0)
        {
            $odd[$i + 1] = $odd[$i] + 1;
            $even[$i + 1] = $even[$i];
        }
 
        // If number is even, increment
        // the count of even frequency
        // leave odd frequency same.
        else
        {
            $even[$i + 1] = $even[$i] + 1;
            $odd[$i + 1] = $odd[$i];
        }
    }
 
    // To solve each query
    for ($i = 0; $i < $Q; $i++)
    {
        $r = $query[$i][2];
        $l = $query[$i][1];
        $k = $query[$i][0];
 
        // Counting total number of
        // element in current query
        $q = $r - $l + 1;
        $p;
 
        // Counting number of odd or
        // even element in current
        // query range
        if ($k > 0)
            $p = $odd[$r] - $odd[$l - 1];
        else
            $p = $even[$r] - $even[$l - 1];
 
        // If frequency is 0, output 0
        if ($p <= 0)
            echo "0" . "\n";
 
        // If frequency is equal to
        // number of element in current
        // range output 1.
        else if ($p == $q)
            echo "1" . "\n";
 
        // Else find the GCD of both.
        // If yes, output by dividing
        // both number by gcd to output
        // the answer in reduced form.
        else
        {
            $g = __gcd($p, $q);
            echo (int)($p / $g) . " " . (int)($q / $g);
        }
    }
}
 
// Driven Program
$arr = array(6, 5, 2, 1, 7);
$n = sizeof($arr);
$Q = 2;
$query = array(array(0, 2, 2),
               array(1, 2, 5));
 
solveQuery($arr, $n, $Q, $query);
 
// This code is contributed
// by Akanksha Rai

Javascript




<script>
// javascript program to find probability
// of even or odd elements in a
// given range.
     
    // Number of tuples in a query
    //static int C = 3;
    // Recursive function to return
    // gcd of a and b
    function  __gcd(a,b)
    {
     
        // Everything divides 0
        if (a == 0 || b == 0)
            return 0;
       
        // base case
        if (a == b)
            return a;
       
        // a is greater
        if (a > b)
            return __gcd(a - b, b);
               
        return __gcd(a, b - a);
    }
     
    // Solve each query of K L R form
    function solveQuery(arr,n,q,query)
    {
     
        // To count number of odd and even
        // number upto i-th index.
        let even = new Array(n + 1);
        let odd = new Array(n + 1);
        even[0] = odd[0] = 0;
       
        // Counting number of odd and even
        // integer upto index i
        for (let i = 0; i < n; i++)
        {
       
            // If number is odd, increment
            // the count of odd frequency
            // leave even frequency same.
            if ((arr[i] & 1) > 0)
            {
                odd[i + 1] = odd[i] + 1;
                even[i + 1] = even[i];
            }
       
            // If number is even, increment
            // the count of even frequency
            // leave odd frequency same.
            else
            {
                even[i + 1] = even[i] + 1;
                odd[i + 1] = odd[i];
            }
        }
       
        // To solve each query
        for (let i = 0; i < Q; i++)
        {
            let r = query[i][2];
            let l = query[i][1];
            let k = query[i][0];
       
            // Counting total number of
            // element in current query
            let q = r - l + 1;
            let p;
       
            // Counting number of odd or
            // even element in current
            // query range
            if (k > 0)
                p = odd[r] - odd[l - 1];
            else
                p = even[r] - even[l - 1];
       
            // If frequency is 0, output 0
            if (p <= 0)
                document.write("0<br>");
       
            // If frequency is equal to
            // number of element in current
            // range output 1.
            else if (p == q)
                document.write("1<br>");
       
            // Else find the GCD of both.
            // If yes, output by dividing
            // both number by gcd to output
            // the answer in reduced form.
            else
            {
                let g = __gcd(p, q);
                document.write(p / g
                          + " " + q / g+"<br>");
            }
        }
    }
     
     // Driven Program
    let arr=[6, 5, 2, 1, 7];
    let n = arr.length;
    let Q = 2;
    let query = [[0, 2, 2 ],[1, 2, 5]];
    solveQuery(arr, n, Q, query);
     
    // This code is contributed by avanitrachhadiya2155
</script>

Output

0
3 4

Time Complexity: O(n)
Auxiliary Space: O(n)

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!