Cake Distribution Problem | C ++ implementation of the approach ; Function to return the remaining count of cakes ; Sum for 1 cycle ; no . of full cycle and remainder ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntCakes ( int n , int m ) { int sum = ( n * ( n + 1 ) ) / 2 ; 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 ) ; } int main ( ) { int n = 3 ; int m = 8 ; int ans = cntCakes ( n , m ) ; cout << ( ans ) ; }