- Related Questions & Answers
- Arranging Coins in C++
- Toss Strange Coins in C++
- Distribute Coins in Binary Tree in C++
- Biggest number by arranging numbers in certain order in C++
- C/C++ Program for Maximum height when coins are arranged in a triangle?
- C/C++ Program for Greedy Algorithm to find Minimum number of Coins
- Probability of getting at least K heads in N tosses of Coins in C++
- Find out the minimum number of coins required to pay total amount in C++
- Minimum number of coins that make a given value
- Program to find the count of coins of each type from the given ratio in C++
- Python Program for Maximum height when coins are arranged in a triangle
- What happens to Bitcoin after all the 21 Million Coins are mined?
- strcoll() in C/C++
- fseek() in C/C++
- strcpy() in C/C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have n coins that we want to form in a staircase shape, every k-th row must have exactly k coins. So if we have n, then we have to find the total number of full staircase rows that can be formed.

So, if the input is like 5, then the output will be 2, as using 5 coins we can make two full starecase rows, the last one needs three, but we have to remain 2 −

* ** **

This can be done directly by using this formula −

$$\frac{\sqrt{(8n+1)}-1}{2}$$

Let us see the following implementation to get a better understanding −

#include <bits/stdc++.h> using namespace std; class Solution { public: int arrangeCoins(int n) { return (sqrt(8*(long long)n+1)-1)/2; } }; main(){ Solution ob; cout << (ob.arrangeCoins(13)); }

13

4

Advertisements