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.

**Examples :**

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.

**Approach :**

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 n^{2} + n – 2*p >= 0.

By applying the formula discussed here, we get n = ceil( (sqrt(8*p+1)-1)/2 )

## C++

`// CPP code to find the bucket to choose ` `// for picking flowers out of it ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `findBucketNo(` `int` `p) ` `{ ` ` ` `return` `ceil` `( ( ` `sqrt` `( 8*p + 1 ) -1 ) / 2 ) ; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `p = 10 ; ` ` ` `cout << findBucketNo(p); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`//Java code to find the bucket to choose ` `// for picking flowers out of it ` `import` `java.lang.System.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `findBucketNo(` `int` `p) ` ` ` `{ ` ` ` `return` `(` `int` `)Math.ceil(( ` ` ` `Math.sqrt( ` `8` `*p + ` `1` `) -` `1` `) / ` `2` `) ; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `p = ` `10` `; ` ` ` ` ` `System.out.println(findBucketNo(p)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 code to find the bucket ` `# to choose for picking flowers ` `# out of it ` `import` `math ` ` ` `def` `findBucketNo(p): ` ` ` ` ` `return` `math.ceil( ( math.sqrt( ` ` ` `8` `*` `p ` `+` `1` `) ` `-` `1` `) ` `/` `2` `) ` ` ` `# Driver code ` `p ` `=` `10` ` ` `print` `(findBucketNo(p)) ` ` ` `# This code is contributed by ` `# Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# code to find the bucket to choose ` `// for picking flowers out of it ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `findBucketNo(` `int` `p) ` ` ` `{ ` ` ` `return` `(` `int` `)Math.Ceiling(( ` ` ` `Math.Sqrt( 8*p + 1 ) -1 ) / 2 ); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `p = 10 ; ` ` ` `Console.WriteLine(findBucketNo(p)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ajit. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code to find the bucket ` `// to choose for picking ` `// flowers out of it ` ` ` `function` `findBucketNo(` `$p` `) ` `{ ` ` ` `return` `ceil` `( ( sqrt( 8 * ` `$p` `+ ` ` ` `1 ) -1 ) / 2 ) ; ` `} ` ` ` `// Driver code ` `$p` `= 10 ; ` `echo` `(findBucketNo(` `$p` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

4

Time Complexity : O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Index of smallest triangular number with N digits
- Check if a larger number divisible by 36
- Highest and Smallest power of K less than and greater than equal to N respectively
- Smallest subarray of size greater than K with sum greater than a given value
- First triangular number whose number of divisors exceeds N
- Find the sequence number of a triangular number
- Find larger of x^y and y^x
- Minimum sum of the elements of an array after subtracting smaller elements from larger
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the larger exponential among two exponentials
- Sum of N-terms of geometric progression for larger values of N | Set 2 (Using recursion)
- Program to print triangular number series till n
- Triangular Matchstick Number
- Squared triangular number (Sum of cubes)
- Centered triangular number
- Centered triangular number in PL/SQL
- Check if a number can be represented as a sum of 2 triangular numbers
- Program to check if N is a Centered Triangular Number
- Smallest even digits number not less than N
- Smallest odd digits number not less than N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.