Sand Timer Flip Counting Problem

Given two integers A and B representing the time taken by two different Sand timers to get empty. The task is to find the count of flips of each timer till the instance at which both the Sand timers get empty at the same time.

Examples:

Input: A = 30, B = 15
Output: 0 1
After 15 minutes: 15 0
Flip timer 2: 15 15
After another 15 minutes: 0 0



Input: A = 10, B = 8
Output: 3 4

Approach: Lowest Common Factor (LCM) of two number will determine the time at which both the Sand timers will get empty together.
LCM(A, B) = (A * B) / GCD(A, B)
Dividing the LCM by input will give the number of flips for each sand timer respectively.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG 
{
  
// Recursive function to return
// the gcd of a and b
static int gcd(int a, int b)
{
    // Everything divides 0
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function to print the number of
// flips for both the sand timers
static void flip(int a, int b)
{
    int lcm = (a * b) / gcd(a, b);
    a = lcm / a;
    b = lcm / b;
    System.out.print((a - 1) + " " + (b - 1));
}
  
// Driver code
public static void main(String[] args) 
{
    int a = 10;
    int b = 5;
    flip(a, b);
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Recursive function to return 
# the gcd of a and b 
def gcd(a, b): 
      
    # Everything divides 0 
    if (b == 0): 
        return
    return gcd(b, a % b) 
  
# Function to print the number of 
# flips for both the sand timers
def flip(a, b):
    lcm = (a * b) // gcd(a, b)
    a = lcm // a
    b = lcm // b
    print(a - 1, b - 1)
  
# Driver code
a = 10
b = 5
flip(a, b)
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
    // Recursive function to return
    // the gcd of a and b
    static int gcd(int a, int b)
    {
        // Everything divides 0
        if (b == 0)
        return a;
          
        return gcd(b, a % b);
    }
      
    // Function to print the number of
    // flips for both the sand timers
    static void flip(int a, int b)
    {
        int lcm = (a * b) / gcd(a, b);
        a = lcm / a;
        b = lcm / b ;
        Console.WriteLine((a - 1) + " " +
                          (b - 1));
    }
      
    // Driver code
    public static void Main()
    {
        int a = 10;
        int b = 5;
        flip(a, b);
    }
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

0 1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.