Friday, December 1, 2017

Arranging Coins

You have a total of N coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given N, find the total number of full staircase rows that can be formed.

Input Format:
The first line contains N.

Output Format:
Single line containing number of full staircase rows.

Boundary Conditions: N is a non-negative integer and fits within the range of a 32-bit signed integer.

Example Input/Output 1:
Input:
5
Output:
2
Explanation:
The coins can form the following rows:
¤
¤ ¤
¤ ¤
Because the 3rd row is incomplete, we return 2.

Solution:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
   long int n;
   cin>>n;
   cout<<(int)((-1 + sqrt(1 + 8 *n))/2);
   return 0;
}
Share: