Open In App

Number of odd and even results for every value of x in range [min, max] after performing N steps

Improve
Improve
Like Article
Like
Save
Share
Report

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. 

  1. 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.
  2. 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.
  3. 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.  

  1. If a is odd, ax is odd. If a is even, ax is even.
  2. 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).
  3. 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++




// C++ program to print
// Number of odd/even results for
// every value of x in range [min, end]
// after performing N steps
#include <bits/stdc++.h>
using namespace std;
 
// Function that prints the
// number of odd and even results
void count_even_odd(int min, int max, int steps[][2])
{
    int a, b, even, odd;
 
    // If constant at layer i is even, beven is true,
    // otherwise false. If the coefficient of x at
    // layer i is even, aeven is true, otherwise false.
    bool beven = true, aeven = false;
    int n = 2;
    for (int i = 0; i < n; i++) {
 
        a = steps[i][0], b = steps[i][1];
 
        // If any of the coefficients at any layer is found
        // to be even, then the product of all the
        // coefficients will always be even.
 
        if (!(aeven || a & 1))
            aeven = true;
 
        // Checking whether the constant added after all
        // layers is even or odd.
 
        if (beven) {
            if (b & 1)
                beven = false;
        }
        else if (!(a & 1)) {
            if (!(b & 1))
                beven = true;
        }
        else {
            if (b & 1)
                beven = true;
        }
    }
 
    // Counting the number of even and odd.
 
    // Assuming input x is even.
    if (beven) {
        even = (int)max / 2 - (int)(min - 1) / 2;
        odd = 0;
    }
    else {
        even = (int)max / 2 - (int)(min - 1) / 2;
        odd = 0;
    }
 
    // Assuming input x is odd.
    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;
 
    // Displaying the counts.
    cout << "even = " << even << ", odd = " << odd << endl;
}
 
// Driver Code
int main()
{
    int min = 1, max = 4;
    int steps[][2] = { { 1, 2 },
                       { 3, 4 } };
 
    count_even_odd(min, max, steps);
    return 0;
}


Java




// Java program to print
// Number of odd/even
// results for every value
// of x in range [min, end]
// after performing N steps
import java.io.*;
 
class GFG
{
 
// Function that prints
// the number of odd and
// even results
static void count_even_odd(int min,
                           int max,
                           int steps[][])
{
    int a, b, even, odd;
 
    // If constant at layer i
    // is even, beven is true,
    // otherwise false. If the
    // coefficient of x at layer
    // i is even, aeven is true,
    // otherwise false.
    boolean beven = true,
            aeven = false;
    int n = 2;
    for (int i = 0; i < n; i++)
    {
 
        a = steps[i][0];
        b = steps[i][1];
 
        // If any of the coefficients
        // at any layer is found to be
        // even, then the product of
        // all the coefficients will
        // always be even.
        if (!(aeven || (a & 1) > 0))
            aeven = true;
 
        // Checking whether the
        // constant added after all
        // layers is even or odd.
        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;
        }
    }
 
    // Counting the number
    // of even and odd.
 
    // Assuming input x is even.
    if (beven)
    {
        even = (int)max / 2 -
               (int)(min - 1) / 2;
        odd = 0;
    }
    else
    {
        even = (int)max / 2 -
               (int)(min - 1) / 2;
        odd = 0;
    }
 
    // Assuming input x is odd.
    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;
 
    // Displaying the counts.
    System.out.print("even = " + even +
                     ", odd = " + odd);
}
 
// Driver Code
public static void main (String[] args)
{
    int min = 1, max = 4;
    int steps[][] = {{1, 2},
                     {3, 4}};
 
    count_even_odd(min, max, steps);
}
}
 
// This code is contributed
// by anuj_67.


Python3




# Python3 program to print
# Number of odd/even results
# for every value of x in
# range [min, end] after
# performing N steps
 
# Function that prints
# the number of odd
# and even results
def count_even_odd(min, max, steps):
  
    # If constant at layer i
    # is even, beven is True,
    # otherwise False. If
    # the coefficient of x at
    # layer i is even, aeven
    # is True, otherwise False.
    beven = True
    aeven = False
    n = 2
    for i in range(0, n) :
        a = steps[i][0]
        b = steps[i][1]
  
        # If any of the coefficients
        # at any layer is found to
        # be even, then the product
        # of all the coefficients
        # will always be even.
        if (not(aeven or a & 1)):
            aeven = True
  
        # Checking whether the
        # constant added after all
        # layers is even or odd.
        if (beven) :
            if (b & 1):
                beven = False
          
        elif (not(a & 1)) :
            if (not(b & 1)):
                beven = True
          
        else :
            if (b & 1):
                beven = True
          
    # Counting the number
    # of even and odd.
  
    # Assuming input x is even.
    if (beven):
        even = (int(max / 2) -
                int((min - 1) / 2))
        odd = 0
      
    else :
        even = (int(max / 2) -
                int((min - 1) / 2))
        odd = 0
  
    # Assuming input x is odd.
    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))
  
    # Displaying the counts.
    print("even = " , even ,
          ", odd = " , odd, sep = "")
  
# Driver Code
min = 1
max = 4
steps = [[1, 2],[3, 4]]
count_even_odd(min, max, steps)
 
# This code is contributed
# by Smitha


C#




// C# program to print
// Number of odd/even
// results for every value
// of x in range [min, end]
// after performing N steps
using System;
 
class GFG
{
 
// Function that prints
// the number of odd and
// even results
static void count_even_odd(int min,
                           int max,
                           int [,]steps)
{
    int a, b, even, odd;
 
    // If constant at layer i
    // is even, beven is true,
    // otherwise false. If the
    // coefficient of x at layer
    // i is even, aeven is true,
    // otherwise false.
    bool beven = true,
         aeven = false;
    int n = 2;
    for (int i = 0; i < n; i++)
    {
 
        a = steps[i, 0];
        b = steps[i, 1];
 
        // If any of the coefficients
        // at any layer is found
        // to be even, then the
        // product of all the
        // coefficients will always
        // be even.
        if (!(aeven || (a & 1) > 0))
            aeven = true;
 
        // Checking whether the
        // constant added after all
        // layers is even or odd.
        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;
        }
    }
 
    // Counting the number
    // of even and odd.
 
    // Assuming input
    // x is even.
    if (beven)
    {
        even = (int)max / 2 -
               (int)(min - 1) / 2;
        odd = 0;
    }
    else
    {
        even = (int)max / 2 -
            (int)(min - 1) / 2;
        odd = 0;
    }
 
    // Assuming input
    // x is odd.
    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;
 
    // Displaying the counts.
    Console.Write("even = " + even +
                  ", odd = " + odd);
}
 
// Driver Code
public static void Main ()
{
    int min = 1, max = 4;
    int [,]steps = {{1, 2},
                    {3, 4}};
 
    count_even_odd(min, max, steps);
}
}
 
// This code is contributed
// by anuj_67.


PHP




<?php
// PHP program to print Number of
// odd/even results for every value
// of x in range [min, end] after
// performing N steps
 
// Function that prints the
// number of odd and even results
function count_even_odd($min, $max, $steps)
{
    // If constant at layer i is even,
    // beven is true, otherwise false.
    // If the coefficient of x at
    // layer i is even, aeven is true,
    // otherwise false.
    $beven = true;
    $aeven = false;
    $n = 2;
    for ($i = 0; $i < $n; $i++)
    {
 
        $a = $steps[$i][0];
        $b = $steps[$i][1];
 
        // If any of the coefficients at
        // any layer is found to be even,
        // then the product of all the
        // coefficients will always be even.
        if (!($aeven || $a & 1))
            $aeven = true;
 
        // Checking whether the constant
        // added after all layers is even or odd.
 
        if ($beven)
        {
            if ($b & 1)
                $beven = false;
        }
        else if (!($a & 1))
        {
            if (!($b & 1))
                $beven = true;
        }
        else
        {
            if ($b & 1)
                $beven = true;
        }
    }
 
    // Counting the number of even and odd.
 
    // Assuming input x is even.
    if ($beven)
    {
        $even = (int)$max / 2 - (int)($min - 1) / 2;
        $odd = 0;
    }
    else
    {
        $even = (int)$max / 2 - (int)($min - 1) / 2;
        $odd = 0;
    }
 
    // Assuming input x is odd.
    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;
 
    // Displaying the counts.
    echo "even = " , $even,
         ", odd = ", $odd, "\n";
}
 
// Driver Code
$min = 1;
$max = 4;
$steps = array( array(1, 2 ),
                array(3, 4 ));
 
count_even_odd($min, $max, $steps);
 
// This code is contributed
// by Ajit Deshpal
?>


Javascript




<script>
 
// Javascript program to print
// Number of odd/even
// results for every value
// of x in range [min, end]
// after performing N steps
 
// Function that prints
// the number of odd and
// even results
function count_even_odd(min, max, steps)
{
    var a, b, even, odd;
     
    // If constant at layer i
    // is even, beven is true,
    // otherwise false. If the
    // coefficient of x at layer
    // i is even, aeven is true,
    // otherwise false.
    var beven = true, aeven = false;
    var n = 2;
     
    for(i = 0; i < n; i++)
    {
        a = steps[i][0];
        b = steps[i][1];
 
        // If any of the coefficients
        // at any layer is found to be
        // even, then the product of
        // all the coefficients will
        // always be even.
        if (!(aeven || (a & 1) > 0))
            aeven = true;
 
        // Checking whether the
        // constant added after all
        // layers is even or odd.
        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;
        }
    }
 
    // Counting the number
    // of even and odd.
 
    // Assuming input x is even.
    if (beven)
    {
        even = parseInt(max / 2) -
               parseInt((min - 1) / 2);
        odd = 0;
    }
    else
    {
        even = parseInt(max / 2) -
               parseInt((min - 1) / 2);
        odd = 0;
    }
 
    // Assuming input x is odd.
    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);
 
    // Displaying the counts.
    document.write("even = " + even + ", odd = " + odd);
}
 
// Driver Code
var min = 1, max = 4;
var steps = [ [ 1, 2 ], [ 3, 4 ] ];
 
count_even_odd(min, max, steps);
 
// This code is contributed by aashish1995
 
</script>


Output: 

even = 2, odd = 2

 

Time Complexity: O(N) where N is the given number of operations.
Auxiliary Space: O(1)



Last Updated : 08 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads