- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Arranging Coins in C++

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}$$

## Example

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)); }

## Input

13

## Output

4

- Related Articles
- Arranging lexicographically and removing whitespaces in JavaScript
- Arranging words in Ascending order in a string - JavaScript
- Biggest number by arranging numbers in certain order in C++
- Arranging words by their length in a sentence in JavaScript
- Toss Strange Coins in C++
- Program to find maximum coins we can get from disappearing coins matrix in Python
- Distribute Coins in Binary Tree in C++
- Sorting or Arranging an Array with standard array values - JavaScript
- I have a total of rupees 300 in coins of denomination rupees 1, 2, 5 the number of rupees 2 coins is 3 times the number of rupees 5 coins. The total number of coins is 160. Find the number of coins of each denomination.
- Program to find number of coins needed to make the changes with given set of coins in Python
- I have a total of $\ Rs. 300$ in coins of denominations $\ Rs. 1,\ Rs. 2$ and $\ Rs. 5$. The number of $\ Rs. 2$ coins is 3 times the number of $\ Rs. 5$ coins. The total number of coins is 160. How many coins of each denomination are with me?
- I have a total of \( Rs. 300 \) in coins of denomination \( Rs. 1, Rs. 2 \) and \( Rs. 5 \). The number of \( Rs. 2 \) coins is 3 times the number of \( Rs. 5 \) coins. The total number of coins is 160 . How many coins of each denomination are with me?
- I have 60 more 1rupee coins than 5 rupee coins. The total value of money is Rs 360. How many 5 rupee coins do I have
- How to find new crypto coins?
- Summing up to amount with fewest coins in JavaScript

Advertisements