Java Program for Maximum height when coins are arranged in a triangle Last Updated : 23 Jul, 2025 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 K kartik Follow 0 Improve K kartik Follow 0 Improve Article Tags : Java Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like