给定两个整数N和K ,其中 N 表示更大的等边三角形的单位大小,任务是找到大小为 K 的等边三角形在 N 边的更大三角形中的数量。
例子:
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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。