# Number of solutions for x < y, where a <= x <= b and c <= y <= d and x, y are integers

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.

Examples:

```Input: a = 2, b = 3, c = 3, d = 4
Output: 3

Input: a = 3, b = 5, c = 6, d = 7
Output: 6
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## C++

 `// C++ implementation of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// function to Find the number of solutions for x < y, ` `// where a <= x <= b and c <= y <= d and x, y integers. ` `int` `NumberOfSolutions(``int` `a, ``int` `b, ``int` `c, ``int` `d) ` `{ ` `    ``// to store answer ` `    ``int` `ans = 0; ` ` `  `    ``// iterate explicitly over all possible values of x ` `    ``for` `(``int` `i = a; i <= b; i++) ` `        ``if` `(d >= max(c, i + 1)) ` `            ``ans += d - max(c, i + 1) + 1; ` ` `  `    ``// return answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a = 2, b = 3, c = 3, d = 4; ` ` `  `    ``// function call ` `    ``cout << NumberOfSolutions(a, b, c, d); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of above approach ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// function to Find the number of ` `// solutions for x < y, where ` `// a <= x <= b and c <= y <= d  ` `// and x, y integers. ` `static` `int` `NumberOfSolutions(``int` `a, ``int` `b,  ` `                             ``int` `c, ``int` `d) ` `{ ` `    ``// to store answer ` `    ``int` `ans = ``0``; ` ` `  `    ``// iterate explicitly over all  ` `    ``// possible values of x ` `    ``for` `(``int` `i = a; i <= b; i++) ` `        ``if` `(d >= Math.max(c, i + ``1``)) ` `            ``ans += d - Math.max(c, i + ``1``) + ``1``; ` ` `  `    ``// return answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `a = ``2``, b = ``3``, c = ``3``, d = ``4``; ` ` `  `    ``// function call ` `    ``System.out.println(NumberOfSolutions(a, b, c, d)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by inder_verma `

## Python 3

 `# Python3 implementation of  ` `# above approach ` ` `  `# function to Find the number of  ` `# solutions for x < y, where  ` `# a <= x <= b and c <= y <= d and ` `# x, y integers.  ` `def` `NumberOfSolutions(a, b, c, d) : ` ` `  `    ``# to store answer  ` `    ``ans ``=` `0` ` `  `    ``# iterate explicitly over all  ` `    ``# possible values of x  ` `    ``for` `i ``in` `range``(a, b ``+` `1``) : ` ` `  `        ``if` `d >``=` `max``(c, i ``+` `1``) : ` ` `  `            ``ans ``+``=` `d ``-` `max``(c, i ``+` `1``) ``+` `1` ` `  `    ``# return answer  ` `    ``return` `ans ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``a, b, c, d ``=` `2``, ``3``, ``3``, ``4` ` `  `    ``# function call  ` `    ``print``(NumberOfSolutions(a, b, c, d)) ` ` `  `# This code is contributed by ANKITRAI1 `

## C#

 `// C# implementation of above approach ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `// function to Find the number of ` `// solutions for x < y, where ` `// a <= x <= b and c <= y <= d  ` `// and x, y integers. ` `static` `int` `NumberOfSolutions(``int` `a, ``int` `b,  ` `                              ``int` `c, ``int` `d) ` `{ ` `    ``// to store answer ` `    ``int` `ans = 0; ` ` `  `    ``// iterate explicitly over all  ` `    ``// possible values of x ` `    ``for` `(``int` `i = a; i <= b; i++) ` `        ``if` `(d >= Math.Max(c, i + 1)) ` `            ``ans += d - Math.Max(c, i + 1) + 1; ` ` `  `    ``// return answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main()  ` `{ ` `    ``int` `a = 2, b = 3, c = 3, d = 4; ` ` `  `    ``// function call ` `    ``Console.WriteLine(NumberOfSolutions(a, b, c, d)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Akanksha Rai(Abby_akku) `

## PHP

 `= max(``\$c``, ``\$i` `+ 1)) ` `            ``\$ans` `+= ``\$d` `- max(``\$c``, ``\$i` `+ 1) + 1; ` ` `  `    ``// return answer ` `    ``return` `\$ans``; ` `} ` ` `  `// Driver code ` `\$a` `= 2; ``\$b` `= 3; ``\$c` `= 3; ``\$d` `= 4; ` ` `  `// function call ` `echo` `NumberOfSolutions(``\$a``, ``\$b``, ``\$c``, ``\$d``); ` ` `  `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` `?> `

Output:

```3
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.