Maximum height when coins are arranged in a triangle
Last Updated :
18 Sep, 2022
We have N coins which need to arrange in form of a triangle, i.e. first row will have 1 coin, second row will have 2 coins and so on, we need to tell maximum height which we can achieve by using these N coins.
Examples:
Input : N = 7
Output : 3
Maximum height will be 3, putting 1, 2 and
then 3 coins. It is not possible to use 1
coin left.
Input : N = 12
Output : 4
Maximum height will be 4, putting 1, 2, 3 and
4 coins, it is not possible to make height as 5,
because that will require 15 coins.
This problem can be solved by finding a relation between height of the triangle and number of coins. Let maximum height is H, then total sum of coin should be less than N,
Sum of coins for height H <= N
H*(H + 1)/2 <= N
H*H + H – 2*N <= 0
Now by Quadratic formula
(ignoring negative root)
Maximum H can be (-1 + ?(1 + 8N)) / 2
Now we just need to find the square root of (1 + 8N) for
which we can use Babylonian method of finding square root
Below code is implemented on above stated concept,
CPP
#include <bits/stdc++.h>
using namespace std;
float squareRoot( float n)
{
float x = n;
float y = 1;
float e = 0.000001;
while (x - y > e)
{
x = (x + y) / 2;
y = n/x;
}
return x;
}
int findMaximumHeight( int N)
{
int n = 1 + 8*N;
int maxH = (-1 + squareRoot(n)) / 2;
return maxH;
}
int main()
{
int N = 12;
cout << findMaximumHeight(N) << endl;
return 0;
}
|
Java
class GFG
{
static float squareRoot( float n)
{
float x = n;
float y = 1 ;
float e = 0 .000001f;
while (x - y > e)
{
x = (x + y) / 2 ;
y = n / x;
}
return x;
}
static int findMaximumHeight( int N)
{
int n = 1 + 8 *N;
int maxH = ( int )(- 1 + squareRoot(n)) / 2 ;
return maxH;
}
public static void main (String[] args)
{
int N = 12 ;
System.out.print(findMaximumHeight(N));
}
}
|
Python3
def squareRoot(n):
x = n
y = 1
e = 0.000001
while (x - y > e):
x = (x + y) / 2
y = n / x
return x
def findMaximumHeight(N):
n = 1 + 8 * N
maxH = ( - 1 + squareRoot(n)) / 2
return int (maxH)
N = 12
print (findMaximumHeight(N))
|
C#
using System;
class GFG
{
static float squareRoot( float n)
{
float x = n;
float y = 1;
float e = 0.000001f;
while (x - y > e)
{
x = (x + y) / 2;
y = n / x;
}
return x;
}
static int findMaximumHeight( int N)
{
int n = 1 + 8*N;
int maxH = ( int )(-1 + squareRoot(n)) / 2;
return maxH;
}
public static void Main()
{
int N = 12;
Console.Write(findMaximumHeight(N));
}
}
|
PHP
<?php
function squareRoot( $n )
{
$x = $n ;
$y = 1;
$e = 0.000001;
while ( $x - $y > $e )
{
$x = ( $x + $y ) / 2;
$y = $n / $x ;
}
return $x ;
}
function findMaximumHeight( $N )
{
$n = 1 + 8 * $N ;
$maxH = (-1 + squareRoot( $n )) / 2;
return floor ( $maxH );
}
$N = 12;
echo findMaximumHeight( $N ) ;
?>
|
Javascript
<script>
function squareRoot(n)
{
let x = n;
let y = 1;
let e = 0.000001;
while (x - y > e)
{
x = (x + y) / 2;
y = n / x;
}
return x;
}
function findMaximumHeight(N)
{
let n = 1 + 8*N;
let maxH = (-1 + squareRoot(n)) / 2;
return Math.round(maxH);
}
let N = 12;
document.write(findMaximumHeight(N));
</script>
|
Output:
4
Time complexity: O(log(n))
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...