Number of odd and even results for every value of x in range [min, max] after performing N steps
Last Updated :
08 Jun, 2022
Given a number N and the min and max range. Given N values of a and b respectively. The task is to count the number of even/odd results after performing a series of N operations as described below.
At every step, calculate:
yN = aNyN-1 + bN.
Explanation:
- Step 1: y1 = a1x + b1
- Step 2: y2 = a2y1 + b2 => y2 = a2a1x + a2b1 + b2
- Step 3: y3 = a3y2 + b3 => y3 = a3a2a1x + a3a2b1 + a3b2 + b3
- Step n: yn = anyn-1 + bn
To obtain the final results, take the values of y0 as every value in the range [min, mix]. For simplicity, we have assumed the value of y0 to be x and replaced x with all possible values in the range [min, max] in the final equation to calculate results.
Examples:
Input: n = 2, min = 1, max = 4
a = 1, b = 2
a = 3, b = 4
Output: even = 2, odd = 2.
Step1: y = 1x + 2 = x+2
Step2: y = 3(x+2) + 4 = 3x + 10
Putting all values of in range [1, 4],
2 odd values and 2 even values are obtained.
Input: n = 1, min = 4, max = 60
a= 1, b = 2
Output: even = 29, odd = 28
A naive approach will be to store the values of a and b in an array and calculate the final result for each number in the specified range. If the result is even, count of even is incremented. Otherwise, count of odd is incremented.
An Efficient Approach which is used here is the basic concept that product of two numbers is even if any one of the numbers is even, otherwise odd, and the sum of two numbers is even only if both the numbers are even. Here, it is seen that at each step, a number is multiplied with x, and another constant is added to the product. The task is to check for the result to be even or odd. At the last step of calculation, check that if a1a2a3…an is even/odd, and a2a3…anb1 + a3a4…anb2 + … + bn is even/odd. Checking if a1a2a3…an is even/odd:
If any ai is even, the product will always be even, otherwise it will be odd. Checking if a2a3…anb1 + a3a4…anb2 + … + bn is even/odd:
Below table explains all the various possibilities for coefficients:
a2a3…ai-1b1 + a3a4…ai-1b2 + … + bi-1 |
ai |
bi |
a2a3…aib1 + a3a4…aib2 + … + bi |
odd |
odd |
odd |
even |
odd |
odd |
even |
odd |
odd |
even |
odd |
odd |
odd |
even |
even |
even |
even |
odd |
odd |
odd |
even |
odd |
even |
even |
even |
even |
odd |
odd |
even |
even |
even |
even |
Below table explains all the various possibilities for y = ax + b:
x |
a |
b |
y |
odd |
odd |
odd |
even |
odd |
odd |
even |
odd |
odd |
even |
odd |
odd |
odd |
even |
even |
even |
even |
odd |
odd |
odd |
even |
odd |
even |
even |
even |
even |
odd |
odd |
even |
even |
even |
even |
Instead of traversing for all the numbers in the range [min, max], divide it into two parts to check whether the number in the range is even or odd as all the even inputs have the same result, and all the odd inputs have the same result. So, check for one case and multiply it by the number of even and odd in the range.
The above calculation is carried out, and the coefficient of x at the last step is checked.
- If it is even, then aeven is true, else false.
- If the constant is even, then beven is true, otherwise false.
- The coefficient of x is even of any one value of a is even in any one layer.
- The constant term after the last layer if executed is checked with the help of value of beven, and current a and b.
With the help of the table given above (first one), the constant at each layer is tested and the value of beven is updated accordingly.
Assume x is even, the value of even and odd is initialized.
- If x is even, then ax will always be even, regardless of a. Hence, depending on the value of the constant term, the result will be even or odd.
- If the constant is even, then the result is even, hence even is initialized by the number of even in the given range ( max/2 – (min-1)/2 ) and odd is initialized by zero.
- If constant is odd, then the result is odd, hence odd is initialized by the number of even in the given range ( max/2 – (min-1)/2 ) and even is initialized by zero.
Assuming x is odd, the value of even and odd is updated.
- If a is odd, ax is odd. If a is even, ax is even.
- If ax and constant, both are odd or ax and constant, both are even, then the result is even, hence even is incremented by the number of odd in the given range ( max – min + 1 – number of even).
- If ax is even and constant is odd or ax is odd and constant is odd, then the result is odd, hence the number of odd is incremented by the number of odd in the given range ( max – min + 1 – number of even).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void count_even_odd( int min, int max, int steps[][2])
{
int a, b, even, odd;
bool beven = true , aeven = false ;
int n = 2;
for ( int i = 0; i < n; i++) {
a = steps[i][0], b = steps[i][1];
if (!(aeven || a & 1))
aeven = true ;
if (beven) {
if (b & 1)
beven = false ;
}
else if (!(a & 1)) {
if (!(b & 1))
beven = true ;
}
else {
if (b & 1)
beven = true ;
}
}
if (beven) {
even = ( int )max / 2 - ( int )(min - 1) / 2;
odd = 0;
}
else {
even = ( int )max / 2 - ( int )(min - 1) / 2;
odd = 0;
}
if (!(beven ^ aeven))
even += max - min + 1 - ( int )max / 2
+ ( int )(min - 1) / 2;
else
odd += max - min + 1 - ( int )max / 2
+ ( int )(min - 1) / 2;
cout << "even = " << even << ", odd = " << odd << endl;
}
int main()
{
int min = 1, max = 4;
int steps[][2] = { { 1, 2 },
{ 3, 4 } };
count_even_odd(min, max, steps);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void count_even_odd( int min,
int max,
int steps[][])
{
int a, b, even, odd;
boolean beven = true ,
aeven = false ;
int n = 2 ;
for ( int i = 0 ; i < n; i++)
{
a = steps[i][ 0 ];
b = steps[i][ 1 ];
if (!(aeven || (a & 1 ) > 0 ))
aeven = true ;
if (beven)
{
if ((b & 1 ) > 0 )
beven = false ;
}
else if (!((a & 1 ) > 0 ))
{
if (!((b & 1 ) > 0 ))
beven = true ;
}
else
{
if ((b & 1 ) > 0 )
beven = true ;
}
}
if (beven)
{
even = ( int )max / 2 -
( int )(min - 1 ) / 2 ;
odd = 0 ;
}
else
{
even = ( int )max / 2 -
( int )(min - 1 ) / 2 ;
odd = 0 ;
}
if (!(beven ^ aeven))
even += max - min + 1 -
( int )max / 2 +
( int )(min - 1 ) / 2 ;
else
odd += max - min + 1 -
( int )max / 2 +
( int )(min - 1 ) / 2 ;
System.out.print( "even = " + even +
", odd = " + odd);
}
public static void main (String[] args)
{
int min = 1 , max = 4 ;
int steps[][] = {{ 1 , 2 },
{ 3 , 4 }};
count_even_odd(min, max, steps);
}
}
|
Python3
def count_even_odd( min , max , steps):
beven = True
aeven = False
n = 2
for i in range ( 0 , n) :
a = steps[i][ 0 ]
b = steps[i][ 1 ]
if ( not (aeven or a & 1 )):
aeven = True
if (beven) :
if (b & 1 ):
beven = False
elif ( not (a & 1 )) :
if ( not (b & 1 )):
beven = True
else :
if (b & 1 ):
beven = True
if (beven):
even = ( int ( max / 2 ) -
int (( min - 1 ) / 2 ))
odd = 0
else :
even = ( int ( max / 2 ) -
int (( min - 1 ) / 2 ))
odd = 0
if ( not (beven ^ aeven)):
even + = ( max - min + 1 -
int ( max / 2 ) + int (( min - 1 ) / 2 ))
else :
odd + = ( max - min + 1 -
int ( max / 2 ) + int (( min - 1 ) / 2 ))
print ( "even = " , even ,
", odd = " , odd, sep = "")
min = 1
max = 4
steps = [[ 1 , 2 ],[ 3 , 4 ]]
count_even_odd( min , max , steps)
|
C#
using System;
class GFG
{
static void count_even_odd( int min,
int max,
int [,]steps)
{
int a, b, even, odd;
bool beven = true ,
aeven = false ;
int n = 2;
for ( int i = 0; i < n; i++)
{
a = steps[i, 0];
b = steps[i, 1];
if (!(aeven || (a & 1) > 0))
aeven = true ;
if (beven)
{
if ((b & 1) > 0)
beven = false ;
}
else if (!((a & 1) > 0))
{
if (!((b & 1) > 0))
beven = true ;
}
else
{
if ((b & 1) > 0)
beven = true ;
}
}
if (beven)
{
even = ( int )max / 2 -
( int )(min - 1) / 2;
odd = 0;
}
else
{
even = ( int )max / 2 -
( int )(min - 1) / 2;
odd = 0;
}
if (!(beven ^ aeven))
even += max - min + 1 -
( int )max / 2 +
( int )(min - 1) / 2;
else
odd += max - min + 1 -
( int )max / 2 +
( int )(min - 1) / 2;
Console.Write( "even = " + even +
", odd = " + odd);
}
public static void Main ()
{
int min = 1, max = 4;
int [,]steps = {{1, 2},
{3, 4}};
count_even_odd(min, max, steps);
}
}
|
PHP
<?php
function count_even_odd( $min , $max , $steps )
{
$beven = true;
$aeven = false;
$n = 2;
for ( $i = 0; $i < $n ; $i ++)
{
$a = $steps [ $i ][0];
$b = $steps [ $i ][1];
if (!( $aeven || $a & 1))
$aeven = true;
if ( $beven )
{
if ( $b & 1)
$beven = false;
}
else if (!( $a & 1))
{
if (!( $b & 1))
$beven = true;
}
else
{
if ( $b & 1)
$beven = true;
}
}
if ( $beven )
{
$even = (int) $max / 2 - (int)( $min - 1) / 2;
$odd = 0;
}
else
{
$even = (int) $max / 2 - (int)( $min - 1) / 2;
$odd = 0;
}
if (!( $beven ^ $aeven ))
$even += $max - $min + 1 - (int) $max / 2 +
(int)( $min - 1) / 2;
else
$odd += $max - $min + 1 - (int) $max / 2 +
(int)( $min - 1) / 2;
echo "even = " , $even ,
", odd = " , $odd , "\n" ;
}
$min = 1;
$max = 4;
$steps = array ( array (1, 2 ),
array (3, 4 ));
count_even_odd( $min , $max , $steps );
?>
|
Javascript
<script>
function count_even_odd(min, max, steps)
{
var a, b, even, odd;
var beven = true , aeven = false ;
var n = 2;
for (i = 0; i < n; i++)
{
a = steps[i][0];
b = steps[i][1];
if (!(aeven || (a & 1) > 0))
aeven = true ;
if (beven)
{
if ((b & 1) > 0)
beven = false ;
}
else if (!((a & 1) > 0))
{
if (!((b & 1) > 0))
beven = true ;
}
else
{
if ((b & 1) > 0)
beven = true ;
}
}
if (beven)
{
even = parseInt(max / 2) -
parseInt((min - 1) / 2);
odd = 0;
}
else
{
even = parseInt(max / 2) -
parseInt((min - 1) / 2);
odd = 0;
}
if (!(beven ^ aeven))
even += max - min + 1 - parseInt(max / 2) +
parseInt((min - 1) / 2);
else
odd += max - min + 1 - parseInt(max / 2) +
parseInt((min - 1) / 2);
document.write( "even = " + even + ", odd = " + odd);
}
var min = 1, max = 4;
var steps = [ [ 1, 2 ], [ 3, 4 ] ];
count_even_odd(min, max, steps);
</script>
|
Output:
even = 2, odd = 2
Time Complexity: O(N) where N is the given number of operations.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...