Given a triangle ABC. H horizontal lines from side AB to AC (as shown in fig.) and V vertical lines from vertex A to side BC are drawn, the task is to find the total no. of triangles formed.
Input: H = 2, V = 2
As we see in the image above, total triangles formed are 18.
Input: H = 3, V = 4
Approach: As we see in the images below, we can derive a general formula for above problem:
- If there are only h horizontal lines then total triangles are (h + 1).
- If there are only v vertical lines then total triangles are (v + 1) * (v + 2) / 2..
- So, total triangles are Triangles formed by horizontal lines * Triangles formed by vertical lines i.e. (h + 1) * (( v + 1) * (v + 2) / 2).
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
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.
- Number of parallelograms when n horizontal parallel lines intersect m vertical parallellines
- Number of triangles formed from a set of points on three lines
- Number of Triangles that can be formed given a set of lines in Euclidean Plane
- Number of horizontal or vertical line segments to connect 3 points
- Vertical and Horizontal retrieval (MRT) on Tapes
- Python - Find the maximum number of triangles with given points on three lines
- Count of intersections of M line segments with N vertical lines in XY plane
- Count the total number of triangles after Nth operation
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Number of triangles that can be formed with given N points
- Number of triangles formed by joining vertices of n-sided polygon with one side common
- Count of different straight lines with total n points with m collinear
- Maximum count of Equilateral Triangles that can be formed within given Equilateral Triangle
- Total ways of choosing X men and Y women from a total of M men and W women
- Count the number of possible triangles
- Number of possible Triangles in a Cartesian coordinate system
- Number of triangles after N moves
- Number of triangles in a plane if no more than two points are collinear
- Count number of right triangles possible with a given perimeter
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
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.