Algorithm
Problem Name: Mathematics -
https://www.hackerrank.com/challenges/lowest-triangle/problem?isFullScreen=true
In this HackerRank in Mathematics -
Given integers b and a, find the smallest integer h, such that there exists a triangle of height h, base b, having an area of at least a.
Example
b = 4
a = 6
The minimum height h is 3 . One example is a triangle formed at points (0, 0), (4, 0), (2, 3).
Function Description
Complete the lowestTriangle function in the editor below.
lowestTriangle has the following parameters:
- int b: the base of the triangle
- int a: the minimum area of the triangle
Returns
int: the minimum integer height to form a triangle with an area of at least a
Input Format
There are two space-separated integers b and a , on a single line.
Constraints
- 1 <= b <= 106
- 1 <=a <= 106
Sample Input 0
2 2
Sample Output 0
2
Explanation 0
Sample Input 1
17 100
Sample Output 1
12
Explanation 1
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,height;
int h,heightceil, heightfloor;
scanf("%f %f",&b,&a);
height = a*2/b;
heightceil = ceil(height), heightfloor = floor(height);
if (heightceil*b/2 >= a){
if (heightfloor*b/2 >= a){
h = heightfloor;
}else{
h = heightceil;
}
}
printf("%d",h);
return 0;
}
Copy The Code &
Try With Live Editor
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
vector < string> split(const string &);
/*
* Complete the 'lowestTriangle' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER trianglebase
* 2. INTEGER area
*/
int lowestTriangle(int b, int a)
{
int h=0,i=1;
while(h==0)
{
if((b*i)/2>=a){h=i;break;} i++;
}
return h;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string first_multiple_input_temp;
getline(cin, first_multiple_input_temp);
vector < string> first_multiple_input = split(rtrim(first_multiple_input_temp));
int trianglebase = stoi(first_multiple_input[0]);
int area = stoi(first_multiple_input[1]);
int height = lowestTriangle(trianglebase, area);
fout << height << "\n";
fout.close();
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun < int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun < int, int>(isspace))).base(),
s.end()
);
return s;
}
vector < string> split(const string &str) {
vector<string> tokens;
string::size_type start = 0;
string::size_type end = 0;
while ((end = str.find(" ", start)) != string::npos) {
tokens.push_back(str.substr(start, end - start));
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}
Copy The Code &
Try With Live Editor
#3 Code Example with C# Programming
Code -
C# Programming
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Result
{
/*
* Complete the 'lowestTriangle' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER trianglebase
* 2. INTEGER area
*/
public static int lowestTriangle(int trianglebase, int area) {
decimal formula=2m*area/trianglebase;
decimal h=Math.Ceiling(formula);
return decimal.ToInt32(h);
}
}
class Solution
{
public static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string[] firstMultipleInput = Console.ReadLine().TrimEnd().Split(' ');
int trianglebase = Convert.ToInt32(firstMultipleInput[0]);
int area = Convert.ToInt32(firstMultipleInput[1]);
int height = Result.lowestTriangle(trianglebase, area);
textWriter.WriteLine(height);
textWriter.Flush();
textWriter.Close();
}
}
Copy The Code &
Try With Live Editor
#4 Code Example with Java Programming
Code -
Java Programming
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'lowestTriangle' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER trianglebase
* 2. INTEGER area
*/
public static int lowestTriangle(int trianglebase, int area) {
return (2*area)/trianglebase + (2*area % trianglebase != 0 ? 1 : 0);
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int trianglebase = Integer.parseInt(firstMultipleInput[0]);
int area = Integer.parseInt(firstMultipleInput[1]);
int height = Result.lowestTriangle(trianglebase, area);
bufferedWriter.write(String.valueOf(height));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Copy The Code &
Try With Live Editor
#5 Code Example with Javascript Programming
Code -
Javascript Programming
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'lowestTriangle' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER trianglebase
* 2. INTEGER area
*/
function lowestTriangle(trianglebase, area) {
const result=Math.ceil(2*area/trianglebase);
return result;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' ');
const trianglebase = parseInt(firstMultipleInput[0], 10);
const area = parseInt(firstMultipleInput[1], 10);
const height = lowestTriangle(trianglebase, area);
ws.write(height + '\n');
ws.end();
}
Copy The Code &
Try With Live Editor
#6 Code Example with Python Programming
Code -
Python Programming
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'lowestTriangle' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER trianglebase
# 2. INTEGER area
#
def lowestTriangle(trianglebase: int, area: int) -> int:
"""Returns the minimum integer height of a triangle with the given area and base."""
triangleheight: int = (area * 2) / trianglebase
if triangleheight.is_integer():
triangleheight = int(triangleheight)
else:
triangleheight = int(triangleheight) + 1
return triangleheight
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
trianglebase = int(first_multiple_input[0])
area = int(first_multiple_input[1])
height = lowestTriangle(trianglebase, area)
fptr.write(str(height) + '\n')
fptr.close()
Copy The Code &
Try With Live Editor