Sand Timer Flip Counting Problem | C ++ 14 implementation of the approach ; Recursive function to return the gcd of a and b ; Everything divides 0 ; Function to print the number of flips for both the sand timers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } void flip ( int a , int b ) { int lcm = ( a * b ) / gcd ( a , b ) ; a = lcm / a ; b = lcm / b ; cout << a - 1 << " ▁ " << b - 1 ; } int main ( ) { int a = 10 ; int b = 5 ; flip ( a , b ) ; }