# Count number of triangles possible for the given sides range

Given four integers **A**, **B**, **C**, and **D**, the task is to find the number of distinct sets (X, Y, and Z) where X, Y and Z denotes the length of sides forming a valid triangle. A ≤ X ≤ B, B ≤ Y ≤ C, and C ≤ Z ≤ D.

**Examples:**

Input:A = 2, B = 3, C = 4, D = 5

Output:7

Explanation:

Possible Length of Side of Triangles are –

{(2, 3, 4), (2, 4, 4), (2, 4, 5), (3, 3, 4), (3, 3, 5), (3, 4, 4) and (3, 4, 5)}

Input:A = 1, B = 1, C = 2, D = 2

Output:1

Explanation:

Only possible length of sides we can choose triangle is (1, 2, 2)

**Naive Approach:** The key observation in the problem is that, If X, Y and Z are the valid sides of a triangle and X ≤ Y ≤ Z, then sufficient conditions for these sides to form a valid triangle will be **X+Y > Z**.

Finally, the count of the possible Z value for the given X and Y can be computed as –

- If X+Y is greater than D, for this case Z can be chosen from [C, D], Total possible values of Z will be (D-C+1).
- If X+Y is less than D and greater than C, then Z can be chosen from [C, X+Y-1].
- If X+Y is less than or equal to C, then we cannot choose Z as these sides will not form a valid triangle.

**Time Complexity:**

**Efficient Approach:** The idea is to iterate for all the possible values of A and then compute the number of possible Y and Z values possible for the given X using mathematical computations.

For a given X, the value of X+Y will be in the range of . If we compute the number of possible value greater than D, then the total number of possible values of Y and Z will be –

// Number of possible values of Y and Z // If num_greater is the number of possible // Y values which is greater than D

Similarly, Let R and L is the upper bound and Lower Bound of the values of X+Y in the range of C and D. Then, total combinations for Y and Z will be –

Below is the implementation of the above approach:

## C++

`// C++ implementation to count the ` `// number of possible triangles ` `// for the given sides ranges ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the number of ` `// possible triangles for the given ` `// sides ranges ` `int` `count_triangles(` `int` `a, ` `int` `b, ` ` ` `int` `c, ` `int` `d) ` `{ ` ` ` ` ` `int` `ans = 0; ` ` ` ` ` `// Iterate for every possible of x ` ` ` `for` `(` `int` `x = a; x <= b; ++x) { ` ` ` ` ` `// Range of y is [b, c] ` ` ` `// From this range First ` ` ` `// we will find the number ` ` ` `// of x + y greater than d ` ` ` `int` `num_greater_than_d = max(d, c + x) - max(d, b + x - 1); ` ` ` ` ` `// For x+y greater than d ` ` ` `// we can choose all z from [c, d] ` ` ` `// Total permutation will be ` ` ` `ans += num_greater_than_d * (d - c + 1); ` ` ` ` ` `// Now we will find the number ` ` ` `// of x+y in between the [c, d] ` ` ` `int` `r = min(max(c, c + x), d) - c; ` ` ` `int` `l = min(max(c, b + x - 1), d) - c; ` ` ` ` ` `// [l, r] will be the range ` ` ` `// from total [c, d] x+y belongs ` ` ` `// For any r such that r = x+y ` ` ` `// We can choose z, in the range ` ` ` `// [c, d] only less than r, ` ` ` `// Thus total permutation be ` ` ` `int` `x1 = (r * (r + 1)) / 2; ` ` ` `int` `x2 = (l * (l + 1)) / 2; ` ` ` ` ` `ans += x1 - x2; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a = 2, b = 3, c = 4, d = 5; ` ` ` ` ` `cout << count_triangles(a, b, c, d) ` ` ` `<< endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to count ` `# the number of possible triangles ` `# for the given sides ranges ` ` ` `# Function to count the number of ` `# possible triangles for the given ` `# sides ranges ` `def` `count_triangles(a, b, c, d): ` ` ` ` ` `ans ` `=` `0` ` ` ` ` `# Iterate for every possible of x ` ` ` `for` `x ` `in` `range` `(a, b ` `+` `1` `): ` ` ` ` ` `# Range of y is [b, c] ` ` ` `# From this range First ` ` ` `# we will find the number ` ` ` `# of x + y greater than d ` ` ` `num_greater_than_d ` `=` `(` `max` `(d, c ` `+` `x) ` `-` ` ` `max` `(d, b ` `+` `x ` `-` `1` `)) ` ` ` ` ` `# For x+y greater than d we ` ` ` `# can choose all z from [c, d] ` ` ` `# Total permutation will be ` ` ` `ans ` `=` `(ans ` `+` `num_greater_than_d ` `*` ` ` `(d ` `-` `c ` `+` `1` `)) ` ` ` ` ` `# Now we will find the number ` ` ` `# of x+y in between the [c, d] ` ` ` `r ` `=` `min` `(` `max` `(c, c ` `+` `x), d) ` `-` `c; ` ` ` `l ` `=` `min` `(` `max` `(c, b ` `+` `x ` `-` `1` `), d) ` `-` `c; ` ` ` ` ` `# [l, r] will be the range ` ` ` `# from total [c, d] x+y belongs ` ` ` `# For any r such that r = x+y ` ` ` `# We can choose z, in the range ` ` ` `# [c, d] only less than r, ` ` ` `# Thus total permutation be ` ` ` `x1 ` `=` `int` `((r ` `*` `(r ` `+` `1` `)) ` `/` `2` `) ` ` ` `x2 ` `=` `int` `((l ` `*` `(l ` `+` `1` `)) ` `/` `2` `) ` ` ` ` ` `ans ` `=` `ans ` `+` `(x1 ` `-` `x2) ` ` ` ` ` `return` `ans ` ` ` `# Driver Code ` `a ` `=` `2` `b ` `=` `3` `c ` `=` `4` `d ` `=` `5` ` ` `print` `(count_triangles(a, b, c, d), end ` `=` `'\n'` `) ` ` ` `# This code is contributed by PratikBasu ` |

*chevron_right*

*filter_none*

**Output:**

7

## Recommended Posts:

- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Count of acute, obtuse and right triangles with given sides
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Find all possible triangles with XOR of sides zero
- Area of the circumcircle of any triangles with sides given
- Count the number of possible triangles
- Count number of unique Triangles using STL | Set 1 (Using set)
- Count number of right triangles possible with a given perimeter
- Count the total number of triangles after Nth operation
- Count number of Unique Triangles using Operator overloading
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Count number of solutions of x^2 = 1 (mod p) in given range
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count number of set bits in a range using bitset
- Count elements in the given range which have maximum number of divisors
- Count of triangles with total n points with m collinear
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Range Queries to count the number of even parity values with updates
- Program to find count of numbers having odd number of divisors in given range
- Count of elements having odd number of divisors in index range [L, R] for Q queries

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.