Queries on probability of even or odd number in given ranges
Last Updated :
20 Jul, 2022
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>
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Range Queries for finding the Sum of all even parity numbers
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 co
10 min read
Check whether a given number is even or odd in PL/SQL
Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Examples: Input: 2 Output: even Input: 5 Output: odd Th
1 min read
Print even and odd numbers in a given range using recursion
Given two integers L and R, the task is to print all the even and odd numbers from L to R using recursion. Examples: Input: L = 1, R = 10Output: Even numbers: 2 4 6 8 10Odd numbers: 1 3 5 7 9 Input: L = 10, R = 25 Output: Even numbers:10 12 14 16 18 20 22 24 Odd numbers:11 13 15 17 19 21 23 25 Appro
6 min read
Check whether product of 'n' numbers is even or odd
Given an array arr[] of size n, the task is to check whether the product of the given n numbers is even or odd. Even product is even, return true, else false.Examples: Input: arr[] = [2, 4, 3, 5]Output: EvenExplanation: Product = 2 * 4 * 3 * 5 = 120, 120 is even.Input: arr[] = [3, 9, 7, 1]Output: Od
5 min read
Check whether XOR of all numbers in a given range is even or odd
Given a range [ L, R ], the task is to find if the value of XOR of all natural numbers in the range L to R ( both inclusive ) is even or odd. Print 'Even' if XOR of all numbers in the range is even, otherwise print odd. Examples: Input: L = 1, R= 10 Output: Odd Input: L= 5, R=15 Output: Even A Simpl
8 min read
Probability of getting a perfect square when a random number is chosen in a given range
Given two integers L and R that denote a range, the task is to find the probability of getting a perfect square number when a random number is chosen in the range L to R. Examples: Input: L = 6, R = 20 Output: 0.133333 Explanation: Perfect squares in range [6, 20] = {9, 16} => 2 perfect squares T
4 min read
Check whether given floating point number is even or odd
Given a floating-point number, check whether it is even or odd. We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number
6 min read
XOR of numbers that appeared even number of times in given Range
Given an array of numbers of size N and Q queries. Each query or a range can be represented by L (LeftIndex) and R(RightIndex). Find the XOR-sum of the numbers that appeared even number of times in the given range. Prerequisite : Queries for number of distinct numbers in given range. | Segment Tree
15+ min read
Sum of range in a series of first odd then even natural numbers
The sequence first consists of all the odd numbers starting from 1 to n and then remaining even numbers starting 2 up to n. Let's suppose we have n as 1000. Then the sequence becomes 1 3 5 7....999 2 4 6....1000We are given a range (L, R), we need to find sum of numbers of this sequence in a given r
7 min read
Check whether a given number is even or odd
Given a number n, check whether it is even or odd. Return true for even and false for odd.Examples: Input: 2 Output: trueInput: 5Output: falseApproach: By Finding the RemainderWe can check the remainder when divided by 2. If the remainder is 0, the number is even; otherwise, it is oddC++// A simple
5 min read