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.
- 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.
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|
Below table explains all the various possibilities for y = ax + b:
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:
even = 2, odd = 2
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Reduce a number to 1 by performing given operations | Set 2
- Number of array elements derivable from D after performing certain operations
- Find the final number obtained after performing the given operation
- Number of elements from the array which are reachable after performing given operations on D
- Number of steps required to convert a binary number to one
- Largest number N which can be reduced to 0 in K steps
- Find the number of stair steps
- Print steps to make a number in form of 2^X - 1
- Find the minimum number of steps to reach M from N
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Number of steps to convert to prime factors
- Minimize the number of steps required to reach the end of the array | Set 2
- Find minimum number of steps to reach the end of String
- Querying maximum number of divisors that a number in a given range has
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.