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 zambiatek!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button