Maximum count of Equilateral Triangles that can be formed within given Equilateral Triangle

Given two integers N and K where N denotes the unit size of a bigger Equilateral Triangle, the task is to find the number of an equilateral triangle of size K that are present in the bigger triangle of side N.
Examples:
Input: N = 4, K = 3
Output: 3
Explanation:
There are 3 equilateral triangles of 3 unit size which are present in the Bigger equilateral triangle of size 4 units.Input: N = 4, K = 2
Output: 7
Explanation:
There are 7 equilateral triangles of 2 unit size which are present in the Bigger equilateral triangle of size 4 units.Â
Naive Approach: The idea is to iterate over all possible sizes of the bigger equilateral triangle for checking the number of triangles with the required size K and print the total count of triangles.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, observe the following points:
- The number of triangles with a peak in the upward direction of size K present in size N equals to ((N – K +1 ) * (N – K + 2))/2.
- The number of inverted triangles with a peak in the downward direction of size K present in size N equals to ((N – 2K + 1) * (N – 2K + 2))/2.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <iostream>using namespace std;Â
// Function to find the number of// equilateral triangle formed// within another triangleint No_of_Triangle(int N, int K){    // Check for the valid condition    if (N < K)        return -1;Â
    else {Â
        int Tri_up = 0;Â
        // Number of triangles having        // upward peak        Tri_up = ((N - K + 1)                  * (N - K + 2))                 / 2;Â
        int Tri_down = 0;Â
        // Number of inverted triangles        Tri_down = ((N - 2 * K + 1)                    * (N - 2 * K + 2))                   / 2;Â
        // Total no. of K sized triangle        return Tri_up + Tri_down;    }}Â
// Driver Codeint main(){Â Â Â Â // Given N and KÂ Â Â Â int N = 4, K = 2;Â
    // Function Call    cout << No_of_Triangle(N, K);    return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{Â
// Function to find the number of// equilateral triangle formed// within another trianglestatic int No_of_Triangle(int N, int K){    // Check for the valid condition    if (N < K)        return -1;Â
    else    {        int Tri_up = 0;Â
        // Number of triangles having        // upward peak        Tri_up = ((N - K + 1) * (N - K + 2)) / 2;Â
        int Tri_down = 0;Â
        // Number of inverted triangles        Tri_down = ((N - 2 * K + 1) *                     (N - 2 * K + 2)) / 2;Â
        // Total no. of K sized triangle        return Tri_up + Tri_down;    }}Â
// Driver Codepublic static void main(String[] args){Â Â Â Â // Given N and KÂ Â Â Â int N = 4, K = 2;Â
    // Function Call    System.out.print(No_of_Triangle(N, K));}}Â
// This code is contributed by PrinciRaj1992 |
Python3
# Python3 program for the above approachÂ
# Function to find the number of# equilateral triangle formed# within another triangledef No_of_Triangle(N, K):       # Check for the valid condition    if (N < K):        return -1;Â
    else:        Tri_up = 0;Â
        # Number of triangles having        # upward peak        Tri_up = ((N - K + 1) *                  (N - K + 2)) // 2;Â
        Tri_down = 0;Â
        # Number of inverted triangles        Tri_down = ((N - 2 * K + 1) *                    (N - 2 * K + 2)) // 2;Â
        # Total no. of K sized triangle        return Tri_up + Tri_down;     # Driver Codeif __name__ == '__main__':    # Given N and K    N = 4; K = 2;Â
    # Function Call    print(No_of_Triangle(N, K));Â
# This code is contributed by sapnasingh4991 |
C#
// C# program for the above approachusing System;class GFG{Â
// Function to find the number of// equilateral triangle formed// within another trianglestatic int No_of_Triangle(int N, int K){    // Check for the valid condition    if (N < K)        return -1;Â
    else    {        int Tri_up = 0;Â
        // Number of triangles having        // upward peak        Tri_up = ((N - K + 1) * (N - K + 2)) / 2;Â
        int Tri_down = 0;Â
        // Number of inverted triangles        Tri_down = ((N - 2 * K + 1) *                     (N - 2 * K + 2)) / 2;Â
        // Total no. of K sized triangle        return Tri_up + Tri_down;    }}Â
// Driver Codepublic static void Main(String[] args){Â Â Â Â // Given N and KÂ Â Â Â int N = 4, K = 2;Â
    // Function Call    Console.Write(No_of_Triangle(N, K));}}Â
// This code is contributed by Rajput-Ji |
Javascript
<script>Â
// JavaScript program for the above approachÂ
// Function to find the number of// equilateral triangle formed// within another trianglefunction No_of_Triangle(N, K){    // Check for the valid condition    if (N < K)        return -1;Â
    else {Â
        let Tri_up = 0;Â
        // Number of triangles having        // upward peak        Tri_up = Math.floor(((N - K + 1)                * (N - K + 2))                / 2);Â
        let Tri_down = 0;Â
        // Number of inverted triangles        Tri_down = Math.floor(((N - 2 * K + 1)                    * (N - 2 * K + 2))                / 2);Â
        // Total no. of K sized triangle        return Tri_up + Tri_down;    }}Â
// Driver CodeÂ
    // Given N and K    let N = 4, K = 2;Â
    // Function Call    document.write(No_of_Triangle(N, K));Â
Â
// This code is contributed by Surbhi Tyagi.Â
</script> |
7
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




