Given two integers **N** and **M**, where N is the number of friends sitting in a clockwise manner in a circle and M is the number of cakes. The task is to calculate the left number of cakes after distributing **i** cakes to **i’th** friend. The distribution of cakes will stop if the count of cakes is less than the required amount.

**Examples:**

Input:N = 4, M = 11

Output:0

1st round:

The 1st friend gets 1 cake, 2nd gets 2 cakes,

3rd get 3 and 4th gets 4 cakes.

Remaining cakes = 11 – (1 + 2 + 3 + 4) = 1

2nd round:

This time only 1st friend gets the left 1 cake.

Remaining cakes = 1 – 1 = 0

Input:N = 3, M = 8

Output:1

1st round:

The 1st friend gets 1 cake, 2nd gets 2 cakes,

and 3rd get 3 cakes.

Remaining cakes = 8 – (1 + 2 + 3) = 2

2nd round:

This time only 1st friend gets the left 1 cake,

and then there is no cake left for 2nd friend.

Remaining cakes = 2 – 1 = 1

**Approach:**

- Check how many cycles of distribution of cakes are possible from m number of cakes.
- Calculate the number of cakes for 1 cycle which is
sum = n * (n + 1) / 2

- Now diving M by sum we get cycle count + some remainder.
- Now check how many remaining cakes are again possible to distribute to x friends.
- The value of x can be easily achieved by solving quadratic equation
remainder = x * (x + 1) / 2

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// remaining count of cakes ` `int` `cntCakes(` `int` `n, ` `int` `m) ` `{ ` ` ` ` ` `// Sum for 1 cycle ` ` ` `int` `sum = (n * (n + 1)) / 2; ` ` ` ` ` `// no. of full cycle and remainder ` ` ` `int` `quo = m/sum ; ` ` ` `int` `rem = m % sum ; ` ` ` `double` `ans = m - quo * sum ; ` ` ` ` ` `double` `x = (-1 + ` `pow` `((8 * rem) + 1, 0.5)) / 2; ` ` ` ` ` `ans = ans - x * (x + 1) / 2; ` ` ` ` ` `return` `int` `(ans); ` `} ` ` ` `// Driver Code ` `int` `main () ` `{ ` ` ` `int` `n = 3; ` ` ` `int` `m = 8; ` ` ` `int` `ans = cntCakes(n, m); ` ` ` `cout << (ans); ` `} ` ` ` `// This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the ` ` ` `// remaining count of cakes ` ` ` `static` `int` `cntCakes(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` ` ` `// Sum for 1 cycle ` ` ` `int` `sum = (n * (n + ` `1` `)) / ` `2` `; ` ` ` ` ` `// no. of full cycle and remainder ` ` ` `int` `quo = m/sum ; ` ` ` `int` `rem = m % sum ; ` ` ` `double` `ans = m - quo * sum ; ` ` ` ` ` `double` `x = (-` `1` `+ Math.pow((` `8` `* rem) + ` `1` `, ` `0.5` `)) / ` `2` `; ` ` ` ` ` `ans = ans - x * (x + ` `1` `) / ` `2` `; ` ` ` ` ` `return` `(` `int` `)ans; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` `int` `m = ` `8` `; ` ` ` `int` `ans = cntCakes(n, m); ` ` ` `System.out.println(ans); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the ` `# remaining count of cakes ` `def` `cntCakes(n, m): ` ` ` ` ` `# Sum for 1 cycle ` ` ` `sum` `=` `(n` `*` `(n ` `+` `1` `))` `/` `/` `2` ` ` ` ` `# no. of full cycle and remainder ` ` ` `quo, rem ` `=` `m` `/` `/` `sum` `, m ` `%` `sum` ` ` `ans ` `=` `m ` `-` `quo ` `*` `sum` ` ` ` ` `x ` `=` `int` `((` `-` `1` `+` `(` `8` `*` `rem ` `+` `1` `)` `*` `*` `0.5` `)` `/` `2` `) ` ` ` `ans ` `=` `ans ` `-` `x` `*` `(x ` `+` `1` `)` `/` `/` `2` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `def` `main(): ` ` ` `n ` `=` `4` ` ` `m ` `=` `11` ` ` `ans ` `=` `cntCakes(n, m) ` ` ` `print` `(ans) ` ` ` `main() ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the ` ` ` `// remaining count of cakes ` ` ` `static` `int` `cntCakes(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` ` ` `// Sum for 1 cycle ` ` ` `int` `sum = (n * (n + 1)) / 2; ` ` ` ` ` `// no. of full cycle and remainder ` ` ` `int` `quo = m/sum ; ` ` ` `int` `rem = m % sum ; ` ` ` `double` `ans = m - quo * sum ; ` ` ` ` ` `double` `x = (-1 + Math.Pow((8 * rem) + 1, 0.5)) / 2; ` ` ` ` ` `ans = ans - x * (x + 1) / 2; ` ` ` ` ` `return` `(` `int` `)ans; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 3; ` ` ` `int` `m = 8; ` ` ` `int` `ans = cntCakes(n, m); ` ` ` `Console.Write(ans); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

**Output:**

0

**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:

- Cake number
- Secretary Problem (A Optimal Stopping Problem)
- Distribution of a Number in Array within a Range
- Standard Normal Distribution (SND) - Java Program
- Converting Power Law Distribution to a Linear graph
- Minimum and maximum number of N chocolates after distribution among K students
- Random number generator in arbitrary probability distribution fashion
- Transportation Problem | Set 7 ( Degeneracy in Transportation Problem )
- Fibonacci problem (Value of Fib(N)*Fib(N) - Fib(N-1) * Fib(N+1))
- Perfect Sum Problem
- 21 Matchsticks Problem
- Tiling Problem
- Transportation Problem | Set 1 (Introduction)
- Transportation Problem | Set 5 ( Unbalanced )
- The Lazy Caterer's Problem
- Frobenius coin problem
- Josephus Problem Using Bit Magic
- Josephus problem | Set 1 (A O(n) Solution)
- The Knight's tour problem | Backtracking-1
- Transportation Problem Set 8 | Transshipment Model-1

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.