Given n number of buckets and each bucket is numbered from 1 to n and flowers in it are equal to triangular numbers. You have to choose the bucket which is left with minimum flower after picking ‘p’ flowers from it.
First bucket contains only 1 flower, second bucket contains 3, third bucket contains 6 and so on following a pattern of n(n+1)/2.
Input : p = 4 Output : bucket 3 Explanation : Buckets with flowers : 1 3 6 10 .... So, bucket 3 is left with only two flowers after selecting p flowers from it which is minimum. Input : p = 10 Output : bucket 4 Explanation : Bucket with flowers : 1 3 6 10 15 ... So, selecting 10 flowers from 4th bucket leave it with 0 flowers.
Observing the input/output of different cases, bucket number can be calculated using formula :
n = ceil( (sqrt(8*p+1)-1)/2 ) ;
How does it work?
We need smallest n such than n*(n+1)/2 >= p
So we need to find roots of equation n2 + n – 2*p >= 0.
By applying the formula discussed here, we get n = ceil( (sqrt(8*p+1)-1)/2 )
Time Complexity : O(1)
- Check if a larger number divisible by 36
- First triangular number whose number of divisors exceeds N
- Triangular Matchstick Number
- Centered triangular number
- Centered triangular number in PL/SQL
- Squared triangular number (Sum of cubes)
- Check if a number can be represented as a sum of 2 triangular numbers
- Program to print triangular number series till n
- Smallest number dividing minimum number of elements in the Array
- Smallest number dividing minimum number of elements in the array | Set 2
- Find the smallest number whose digits multiply to a given number n
- Smallest number by rearranging digits of a given number
- Immediate smallest number after re-arranging the digits of a given number
- Find larger of x^y and y^x
- Find the larger exponential among two exponentials
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.