Given a pile of chocolates and an integer ‘k’ i.e. the number of iterations, the task is to find the number of chocolates left after k iterations.

**Note:** In every iteration, we can choose a pile with a maximum number of chocolates, after that square root of chocolate remains and rest is eaten.

**Examples:**

Input: Chocolates = 100000000, Iterations = 3 Output: 10 Input: Chocolates = 200, Iterations = 2 Output: 4

**Note:** Output is printed after rounding off the value as in the 2nd example, the output will be around **3.76 approx.**

**Approach:**

It is given that maximum no. of chocolates are selected so consider total pile since it will be maximum.

Next, it is given that in the each iteration only square of chocolates are left so that by considering the mathematics equation of

(((number)^{n})^{n})...^{n}for k times = (number)^{nk}

Since here k times the square root is performed so the (1/2)^{k} is powered with the N.

Consider the example of 100000000 chocolates and no. of iterations is 3 then it will be as

(((100000000)^{1/2})^{1/2})^{1/2}= (100000000)^{(1/2)3}= 10

**Below is the required formula to find the remaining chocolates:**

round(pow(n, (1.0/pow(2, k))))

## C++

`// C++ program to find remaining ` `// chocolates after k iterations ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the chocolates left ` `int` `results(` `int` `n, ` `int` `k) ` `{ ` ` ` `return` `round(` `pow` `(n, (1.0 / ` `pow` `(2, k)))); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `k = 3, n = 100000000; ` ` ` ` ` `cout << ` `"Chocolates left after "` `<< k ` ` ` `<< ` `" iterations are "` `<< results(n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find remaining ` `// chocolates after k iterations ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` `// Function to find the ` `// chocolates left ` `static` `int` `results(` `int` `n, ` `int` `k) ` `{ ` ` ` `return` `(` `int` `)Math.round(Math.pow(n, ` ` ` `(` `1.0` `/ Math.pow(` `2.0` `, k)))); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `k = ` `3` `, n = ` `100000000` `; ` ` ` ` ` `System.out.print(` `"Chocolates left after "` `+ k + ` ` ` `" iterations are "` `+ results(n, k)); ` `} ` `} ` ` ` `// This code is contributed by Subhadeep ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find remaining ` `// chocolates after k iterations ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to find the ` `// chocolates left ` `static` `int` `results(` `int` `n, ` `int` `k) ` `{ ` ` ` `return` `(` `int` `)Math.Round(Math.Pow(n, ` ` ` `(1.0 / Math.Pow(2.0, k)))); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `k = 3, n = 100000000; ` ` ` ` ` `Console.Write(` `"Chocolates left after "` `+ ` ` ` `k + ` `" iterations are "` `+ ` ` ` `results(n, k)); ` `} ` `} ` ` ` `// This code is contributed ` `// by ChitraNayal ` |

*chevron_right*

*filter_none*

## Python

`# Python program to find ` `# remaining chocolates ` `# after k iterations ` ` ` `# Function to find the ` `# chocolates left ` `def` `results(n, k): ` ` ` ` ` `return` `round` `(` `pow` `(n, (` `1.0` `/` ` ` `pow` `(` `2` `, k)))) ` ` ` `# Driver code ` `k ` `=` `3` `n ` `=` `100000000` ` ` `print` `(` `"Chocolates left after"` `), ` `print` `(k), ` `print` `(` `"iterations are"` `), ` `print` `(` `int` `(results(n, k))) ` ` ` `# This code is contributed ` `# by Shivi_Aggarwal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find remaining ` `// chocolates after k iterations ` ` ` `// Function to find ` `// the chocolates left ` `function` `results(` `$n` `, ` `$k` `) ` `{ ` ` ` `return` `round` `(pow(` `$n` `, (1.0 / ` ` ` `pow(2, ` `$k` `)))); ` `} ` ` ` `// Driver code ` `$k` `= 3; ` `$n` `= 100000000; ` `echo` `(` `"Chocolates left after "` `); ` `echo` `(` `$k` `); ` ` ` `echo` `(` `" iterations are "` `); ` `echo` `(results(` `$n` `, ` `$k` `)); ` ` ` `// This code is contributed ` `// by Shivi_Aggarwal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Chocolates left after 3 iterations are 10

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:

- Minimum and maximum number of N chocolates after distribution among K students
- Puzzle | Selling Chocolates
- Fill array with 1's using minimum iterations of filling neighbors
- Find the minimum number of rectangles left after inserting one into another
- Position after taking N steps to the right and left in an alternate manner
- Find the index of the left pointer after possible moves in the array
- Find the Mth element of the Array after K left rotations
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- How to add space (" ") after an element using :after selector in CSS ?
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Longest subsequence of a number having same left and right rotation
- Number formed by flipping all bits to the left of rightmost set bit
- Count number of digits after decimal on dividing a number
- Immediate smallest number after re-arranging the digits of a given number
- Count all possible paths from top left to bottom right of a mXn matrix
- Left-Truncatable Prime
- Color N boxes using M colors such that K boxes have different color from the box on its left
- CSS | padding-left Property
- CSS | border-left-color Property
- CSS | border-left-width Property

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.