Total number of triangles formed when there are H horizontal and V vertical lines

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.
Examples:Ā
Ā
Input: H = 2, V = 2Ā
Output: 18Ā
Ā
As we see in the image above, total triangles formed are 18.
Input: H = 3, V = 4Ā
Output: 60Ā
Ā
Ā
Ā
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:
Ā
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;#define LLI long long intĀ
// Function to return total trianglesLLI totalTriangles(LLI h, LLI v){Ā Ā Ā Ā // Only possible triangle isĀ Ā Ā Ā // the given triangleĀ Ā Ā Ā if (h == 0 && v == 0)Ā Ā Ā Ā Ā Ā Ā Ā return 1;Ā
Ā Ā Ā Ā // If only vertical lines are presentĀ Ā Ā Ā if (h == 0)Ā Ā Ā Ā Ā Ā Ā Ā return ((v + 1) * (v + 2) / 2);Ā
Ā Ā Ā Ā // If only horizontal lines are presentĀ Ā Ā Ā if (v == 0)Ā Ā Ā Ā Ā Ā Ā Ā return (h + 1);Ā
Ā Ā Ā Ā // Return total trianglesĀ Ā Ā Ā LLI Total = (h + 1) * ((v + 1) * (v + 2) / 2);Ā
Ā Ā Ā Ā return Total;}Ā
// Driver codeint main(){Ā Ā Ā Ā int h = 2, v = 2;Ā Ā Ā Ā cout << totalTriangles(h, v);Ā
Ā Ā Ā Ā return 0;} |
Java
// Java implementation of the approachclass GFG {Ā
Ā Ā Ā Ā // Function to return total trianglesĀ Ā Ā Ā public static int totalTriangles(int h, int v)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // Only possible triangle isĀ Ā Ā Ā Ā Ā Ā Ā // the given triangleĀ Ā Ā Ā Ā Ā Ā Ā if (h == 0 && v == 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 1;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If only vertical lines are presentĀ Ā Ā Ā Ā Ā Ā Ā if (h == 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return ((v + 1) * (v + 2) / 2);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If only horizontal lines are presentĀ Ā Ā Ā Ā Ā Ā Ā if (v == 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return (h + 1);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return total trianglesĀ Ā Ā Ā Ā Ā Ā Ā int total = (h + 1) * ((v + 1) * (v + 2) / 2);Ā
Ā Ā Ā Ā Ā Ā Ā Ā return total;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driver codeĀ Ā Ā Ā public static void main(String[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int h = 2, v = 2;Ā Ā Ā Ā Ā Ā Ā Ā System.out.print(totalTriangles(h, v));Ā Ā Ā Ā }} |
C#
// C# implementation of the approach using System;Ā
class GFG { Ā
Ā Ā Ā Ā // Function to return total triangles Ā Ā Ā Ā public static int totalTriangles(int h, int v) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā // Only possible triangle is Ā Ā Ā Ā Ā Ā Ā Ā // the given triangle Ā Ā Ā Ā Ā Ā Ā Ā if (h == 0 && v == 0) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 1; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If only vertical lines are present Ā Ā Ā Ā Ā Ā Ā Ā if (h == 0) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return ((v + 1) * (v + 2) / 2); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If only horizontal lines are present Ā Ā Ā Ā Ā Ā Ā Ā if (v == 0) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return (h + 1); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return total triangles Ā Ā Ā Ā Ā Ā Ā Ā int total = (h + 1) * ((v + 1) * (v + 2) / 2); Ā
Ā Ā Ā Ā Ā Ā Ā Ā return total; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Driver code Ā Ā Ā Ā public static void Main() Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int h = 2, v = 2; Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(totalTriangles(h, v)); Ā Ā Ā Ā } } Ā
// This code is contributed by Ryuga |
Python3
# Python3 implementation of the approachĀ
# Function to return total trianglesdef totalTriangles(h, v):Ā Ā Ā Ā Ā Ā Ā Ā Ā # Only possible triangle is Ā Ā Ā Ā # the given triangleĀ Ā Ā Ā if (h == 0 and v == 0):Ā Ā Ā Ā Ā Ā Ā Ā return 1Ā
Ā Ā Ā Ā # If only vertical lines are presentĀ Ā Ā Ā if (h == 0):Ā Ā Ā Ā Ā Ā Ā Ā return ((v + 1) * (v + 2) / 2)Ā
Ā Ā Ā Ā # If only horizontal lines are presentĀ Ā Ā Ā if (v == 0):Ā Ā Ā Ā Ā Ā Ā Ā return (h + 1)Ā
Ā Ā Ā Ā # Return total trianglesĀ Ā Ā Ā total = (h + 1) * ((v + 1) * (v + 2) / 2)Ā
Ā Ā Ā Ā return totalĀ
# Driver codeh = 2v = 2print(int(totalTriangles(h, v))) |
PHP
<?php// PHP implementation of the above approachĀ
// Function to return total triangles function totalTriangles($h, $v) { Ā Ā Ā Ā // Only possible triangle is Ā Ā Ā Ā // the given triangle Ā Ā Ā Ā if ($h == 0 && $v == 0) Ā Ā Ā Ā Ā Ā Ā Ā return 1; Ā
Ā Ā Ā Ā // If only vertical lines are present Ā Ā Ā Ā if ($h == 0) Ā Ā Ā Ā Ā Ā Ā Ā return (($v + 1) * ($v + 2) / 2); Ā
Ā Ā Ā Ā // If only horizontal lines are present Ā Ā Ā Ā if ($v == 0) Ā Ā Ā Ā Ā Ā Ā Ā return ($h + 1); Ā
Ā Ā Ā Ā // Return total triangles Ā Ā Ā Ā $Total = ($h + 1) * (($v + 1) * Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ($v + 2) / 2); Ā
Ā Ā Ā Ā return $Total; } Ā
// Driver code $h = 2;$v = 2; echo totalTriangles($h, $v); Ā
// This code is contributed by Arnab Kundu?> |
Javascript
<script>Ā
// javascript implementation of the approachĀ Ā // Function to return total trianglesĀ
function totalTriangles(h , v){Ā Ā Ā Ā // Only possible triangle isĀ Ā Ā Ā // the given triangleĀ Ā Ā Ā if (h == 0 && v == 0)Ā Ā Ā Ā Ā Ā Ā Ā return 1;Ā
Ā Ā Ā Ā // If only vertical lines are presentĀ Ā Ā Ā if (h == 0)Ā Ā Ā Ā Ā Ā Ā Ā return ((v + 1) * (v + 2) / 2);Ā
Ā Ā Ā Ā // If only horizontal lines are presentĀ Ā Ā Ā if (v == 0)Ā Ā Ā Ā Ā Ā Ā Ā return (h + 1);Ā
Ā Ā Ā Ā // Return total trianglesĀ Ā Ā Ā var total = (h + 1) * ((v + 1) * (v + 2) / 2);Ā
Ā Ā Ā Ā return total;}Ā
// Driver codevar h = 2, v = 2;document.write(totalTriangles(h, v));Ā
// This code contributed by shikhasingrajputĀ
</script> |
Output:Ā
18
Ā
Time Complexity: O(1)Ā
Auxiliary Space: O(1)
Ā
Feeling lost in the world of random DSA topics, wasting time without progress? Itās time for a change! Join our DSA course, where weāll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



