Given four integers a, b, c, d ( upto 10^6 ). The task is to Find the number of solutions for x < y, where a <= x <= b and c <= y <= d and x, y integers.
Input: a = 2, b = 3, c = 3, d = 4 Output: 3 Input: a = 3, b = 5, c = 6, d = 7 Output: 6
Approach: Let’s iterate explicitly over all possible values of x. For one such fixed value of x, the problem reduces to how many values of y are there such that c <= y <= d and x = max(c, x + 1) and y <= d. Let’s assume that c <= d, otherwise, there are no valid values of y of course. It follows, that for a fixed x, there are d – max(c, x+1) + 1 valid values of y because the number of integers in a range [R1, R2] is given by R2 – R1 + 1.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find number of solutions of a linear equation of n variables
- Number of non-negative integral solutions of a + b + c = n
- Count number of solutions of x^2 = 1 (mod p) in given range
- Number of integral solutions of the equation x1 + x2 +.... + xN = k
- Number of non-negative integral solutions of sum equation
- Number of solutions to Modular Equations
- Number of integral solutions for equation x = b*(sumofdigits(x)^a)+c
- Program to find number of solutions in Quadratic Equation
- Find 'N' number of solutions with the given inequality equations
- Number of solutions of n = x + n ⊕ x
- Number of solutions for the equation x + y + z <= n
- Find the number of solutions to the given equation
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Drishti-Soft Solutions Interview | Set 1
- Python | Finding Solutions of a Polynomial Equation
- 0/1 Knapsack Problem to print all possible solutions
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find the number of positive integers less than or equal to N that have an odd number of digits
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.