Split given isosceles triangle of height H into N equal parts
Given an integer N and an isosceles triangle consisting of height H, the task is to find (N – 1) points on the triangle such that the line passing through these points and parallel to the base of the triangle, divide the total area into N equal parts.
Examples:
Input: N = 3, H = 2
Output: 1.15 1.63
Explanation: Make cuts at point 1.15 and 1.63 as shown below:
Input: N = 2, H = 1000
Output: 70710.67
Approach: The problem can be solved by observing the following properties:
Divide the triangle such that (xi / h)2 = i / N
=> xi = h*?(i/n)
xi = height of ith cut from the top vertex of the triangle
Follow the steps below to solve the problem:
- Iterate over the range [1, N – 1].
- In every ith iteration, print the value of xi using the above formula.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPoint( int n, int h)
{
for ( int i = 1; i < n; i++)
printf ( "%.2f " , sqrt (i / (n*1.0)) * h);
}
int main()
{
int n = 3;
int h = 2;
findPoint(n, h);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findPoint( int n, int h)
{
for ( int i = 1 ; i < n; i++)
System.out.printf( "%.2f " ,
Math.sqrt(i / (n * 1.0 )) * h);
}
public static void main(String[] args)
{
int n = 3 ;
int h = 2 ;
findPoint(n, h);
}
}
|
Python3
def findPoint(n, h):
for i in range ( 1 , n):
print ( "{0:.2f}" . format (((i / n) * * 0.5 ) * h), end = ' ' )
if __name__ = = '__main__' :
n = 3
h = 2
findPoint(n, h)
|
C#
using System;
class GFG
{
static void findPoint( int n, int h)
{
for ( int i = 1; i < n; i++)
Console.Write( "{0:F2} " ,
Math.Sqrt(i / (n * 1.0)) * h);
}
public static void Main(String[] args)
{
int n = 3;
int h = 2;
findPoint(n, h);
}
}
|
Javascript
<script>
function findPolet(n, h)
{
for (let i = 1; i < n; i++)
document.write(
Math.sqrt(i / (n * 1.0)) * h + " " );
}
let n = 3;
let h = 2;
findPolet(n, h);;
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
18 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...