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.
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,
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Puzzle | Connect 9 circles each arranged at center of a Matrix using 3 straight lines
- Minimum height of a triangle with given base and area
- Find the height of a right-angled triangle whose area is X times its base
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Maximum height of triangular arrangement of array values
- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
- Maximum path sum in a triangle.
- Maximum area of triangle having different vertex colors
- Maximum sum of a path in a Right Number Triangle
- Maximum number of squares that can fit in a right angle isosceles triangle
- Maximum Perimeter Triangle from array
- Maximum path sum in an Inverted triangle | SET 2
- Maximum area of rectangle inscribed in an equilateral triangle
- Maximum count of Equilateral Triangles that can be formed within given Equilateral Triangle
- Radius of the circle when the width and height of an arc is given
- Percentage increase in the cylinder if the height is increased by given percentage but radius remains constant
- Count triangles required to form a House of Cards of height N
- Check whether a given point lies inside a triangle or not
- Count Integral points inside a Triangle
Improved By : vt_m