Java Program for Maximum height when coins are arranged in a triangle Last Updated : 05 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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. Java // Java program to find maximum height // of arranged coin triangle class GFG { /* Returns the square root of n. Note that the function */ static float squareRoot(float n) { /* We are using n itself as initial approximation.This can definitely be improved */ float x = n; float y = 1; // e decides the accuracy level float e = 0.000001f; while (x - y > e) { x = (x + y) / 2; y = n / x; } return x; } // Method to find maximum height // of arrangement of coins static int findMaximumHeight(int N) { // calculating portion inside // the square root int n = 1 + 8 * N; int maxH = (int)(-1 + squareRoot(n)) / 2; return maxH; } // Driver code public static void main(String[] args) { int N = 12; System.out.print(findMaximumHeight(N)); } } // This code is contributed by Anant Agarwal. Output: 4 Please refer complete article on Maximum height when coins are arranged in a triangle for more details! Comment More infoAdvertise with us Next Article Java Program for Maximum height when coins are arranged in a triangle K kartik Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Java Program for Maximum sum rectangle in a 2D matrix | DP-27 Write a Java program for a given 2D array, the task is to find the maximum sum subarray in it. For example, in the following 2D array, the maximum sum subarray is highlighted with blue rectangle and sum of this subarray is 29. This problem is mainly an extension of the Largest Sum Contiguous Subarra 5 min read Java Program for Coin Change Write a Java program for a given integer array of coins[ ] of size N representing different types of denominations and an integer sum, the task is to count the number of coins required to make a given value sum. Examples: Input: sum = 4, coins[] = {1,2,3}, Output: 4Explanation: there are four soluti 8 min read Java Program for Triangular Matchstick Number Given a number X which represents the floor of a matchstick pyramid, write a program to print the total number of matchstick required to form a pyramid of matchsticks of x floors. Examples: Input : X = 1Output : 3 Input : X = 2Output : 9 This is mainly an extension of triangular numbers. For a numbe 1 min read Java Program for Coin Change | DP-7 Write a Java program for a given integer array of coins[ ] of size N representing different types of denominations and an integer sum, the task is to find the number of ways to make a sum by using different denominations. Examples: Input: sum = 4, coins[] = {1,2,3}, Output: 4Explanation: there are f 8 min read Java Program for Program to calculate volume of a Tetrahedron A Tetrahedron is simply a pyramid with a triangular base. It is a solid object with four triangular faces, three on the sides or lateral faces, one on the bottom of the base and four vertices or corners. If the faces are all congruent equilateral triangles, then the tetrahedron is called regular. Th 1 min read Like