Thursday, October 9, 2025
HomeData Modelling & AITotal number of triangles formed when there are H horizontal and V...

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: 
 

  1. If there are only h horizontal lines then total triangles are (h + 1).
  2. If there are only v vertical lines then total triangles are (v + 1) * (v + 2) / 2.
     

  1. 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 triangles
LLI 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 code
int main()
{
    int h = 2, v = 2;
    cout << totalTriangles(h, v);
 
    return 0;
}


Java




// Java implementation of the approach
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(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 triangles
def 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 code
h = 2
v = 2
print(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 code
var 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 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS