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
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check if the last element of array is even or odd after performing a operation p times
- Check if a number has an odd count of odd divisors and even count of even divisors
- Find the value of max(f(x)) - min(f(x)) for a given F(x)
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Check if left and right shift of any string results into given string
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Number formed by adding product of its max and min digit K times
- Count of operations to make all elements of array a equal to its min element by performing a[i] – b[i]
- Minimum N-Digit number required to obtain largest N-digit number after performing given operations
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Sum of width (max and min diff) of all Subsequences
- Partition N into M parts such that difference between Max and Min part is smallest
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Min and max length subarray having adjacent element difference atmost K
- Longest subarray such that difference of max and min is at-most K
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Remove minimum elements from the array such that 2*min becomes more than max
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.