Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Program for Maximum height when coins are arranged in a triangle
Next article icon

Python Program for Maximum height when coins are arranged in a triangle

Last Updated : 21 Feb, 2023
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
Explanation: 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
Explanation: 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.

Brute-Force Approach:
A triangle takes coins in increasing order i.e., first line will have 1 coin, second line will have 2 coins and so on.
So we can subtract [1.2.3.,,,] till n is greater than the number, and increment our answer.

Below is the implementation of the above approach:

Python3
def triangle(input1):     if input1 < 1:         return 'Zero'     else:           # Counter for controlling the coins in a row         a = 1                 height = 0         while input1 > 0:             if input1 - a >= 0:                 input1 = input1 - a                 a = a + 1                 height = height + 1             else:                 break         return height   if __name__ == "__main__":     testinput1 = 22     print(triangle(testinput1)) 

Output
6

Time complexity: O(sqrt(n))
Auxiliary space: O(1).

Efficient Approach: 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

Python3
# Python3 program to find # maximum height of arranged # coin triangle  # Returns the square root of n. # Note that the function  def squareRoot(n):       # We are using n itself as         # initial approximation     # This can definitely be improved      x = n      y = 1       e = 0.000001  # e decides the accuracy level      while (x - y > e):         x = (x + y) / 2         y = n/x              return x     # Method to find maximum height # of arrangement of coins def findMaximumHeight(N):       # calculating portion inside the square root     n = 1 + 8*N      maxH = (-1 + squareRoot(n)) / 2     return int(maxH)     # Driver code to test above method N = 12  print(findMaximumHeight(N))  # This code is contributed by # Smitha Dinesh Semwal 

Output
4

Time complexity: O(log N)
Auxiliary space: O(1)

Please refer complete article on Maximum height when coins are arranged in a triangle for more details!


Next Article
Python Program for Maximum height when coins are arranged in a triangle

K

kartik
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

    Minimum height of a triangle with given base and area
    Given two integers a and b, find the smallest possible height such that a triangle of at least area "a" and base "b" can be formed.Examples: Input : a = 2, b = 2Output : Minimum height of triangle is 2Explanation: Input : a = 8, b = 4Output : Minimum height of triangle is 4Minimum height of Triangle
    5 min read
    Maximum Depth or Height Of a Binary Tree with python
    Binary trees are hierarchical data structures that have widespread applications in computer science, from databases to graphics. One essential property of a binary tree is its depth or height. In this article, we'll discuss how to compute the maximum depth (or height) of a binary tree using Python.
    5 min read
    Python Program for Coin Change | DP-7
    Write a Python 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
    7 min read
    Find the maximum and minimum element in a NumPy array
    An array can be considered as a container with the same types of elements. Python has its array module named array. We can simply import the module and create our array. But this module has some of its drawbacks. The main disadvantage is we can't create a multidimensional array. And the data type mu
    4 min read
    Python Program for Cutting a Rod | DP-13
    Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of the rod is 8 and the values of different pieces are given as following, then
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences