给定两个整数N和K ,其中N表示较大的等边三角形的单位大小,任务是找到侧面N的较大三角形中存在的大小为K的等边三角形的数目。
例子:
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.
天真的方法:想法是遍历较大的等边三角形的所有可能大小,以检查具有所需大小K的三角形的数量,并打印三角形的总数。
时间复杂度: O(N)
辅助空间: O(1)
高效方法:要优化上述方法,请注意以下几点:
- 在大小为N的大小为K的向上具有峰值的三角形的数量等于((N-K +1)*(N-K + 2))/ 2 。
- 在大小为N的大小为K的向下方向上具有峰值的倒三角形的数量等于((N – 2K +1)*(N – 2K + 2))/ 2 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of
// equilateral triangle formed
// within another triangle
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 Code
int 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 approach
import java.util.*;
class GFG{
// Function to find the number of
// equilateral triangle formed
// within another triangle
static 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 Code
public 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 triangle
def 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 Code
if __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 approach
using System;
class GFG{
// Function to find the number of
// equilateral triangle formed
// within another triangle
static 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 Code
public 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
输出:
7
时间复杂度: O(1)
辅助空间: O(1)