Find a time for which angle between hour and minute hands is given theta

Given angle theta, find a possible time (in hh:mm format) when angle between hour hand and clock hand is theta. If no such time exists then print -1.

Examples :

Input : theta = 90.0
Output : 3:0
Input : theta = 60.0
Output : 2:0

We have discussed how to find angle for given time in below post.

Calculate the angle between hour hand and minute hand

In this problem, we are asked to do revers. Since there are 12 possibilities for hour and 60 possibilities for minute, we loop through all possible time which is 12*60 = 720, if angle for any time is equal to given theta then we print that time.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find time for a given angle.
#include <bits/stdc++.h>
using namespace std;
  
// function to find angle between
// hour hand and minute hand
float calcAngle(int hh, int mm)
{
    // Calculate the angles moved by hour and
    // minute hands with reference to 12:00
    float hour_angle = 0.5 * (hh*60 + mm);
    float minute_angle = 6*mm;
  
    // Find the difference between two angles
    float angle = abs(hour_angle - minute_angle);
  
    // Return the smaller angle of two possible
    // angles
    angle = min(360-angle, angle);
  
    return angle;
}
  
// function to print all time when angle between
// hour hand and minute hand is theta
void printTime(float theta)
{
    for (int hh=0; hh<12; hh++)
    {
        for (int mm=0; mm<60; mm++)
        {
            if (calcAngle(hh, mm)==theta)
            {
                printf("%d:%d\n", hh, mm);
                return;
            }
        }
    }
    printf("Input angle not valid.\n");
    return;
}
  
// driver code to test ablove function
int main()
{
    float theta = 90.0;
    printTime(theta);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find time
// for a given angle.
class GFG
{
// function to find angle between
// hour hand and minute hand
static float calcAngle(int hh, int mm)
{
    // Calculate the angles moved by hour and
    // minute hands with reference to 12:00
    float hour_angle = 0.5f * (hh * 60 + mm);
    float minute_angle = 6 * mm;
  
    // Find the difference between two angles
    float angle = Math.abs(hour_angle - minute_angle);
  
    // Return the smaller angle of 
    // two possible angles
    angle = Math.min(360-angle, angle);
  
    return angle;
}
  
// function to print all time when 
// angle between hour hand and minute
// hand is theta
static void printTime(float theta)
{
    for (int hh = 0; hh < 12; hh++)
    {
        for (int mm = 0; mm < 60; mm++)
        {
            if (calcAngle(hh, mm) == theta)
            {
                System.out.println(hh + ":" + mm);
                return;
            }
        }
    }
      
    System.out.println("Input angle not valid.");
    return;
  
// Driver code 
public static void main (String[] args)
{
    float theta = 90.0f;
    printTime(theta);
}
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find time for a
# given angle.
  
# function to find angle between
# hour hand and minute hand
def calcAngle(hh, mm):
  
    # Calculate the angles moved by
    # hour and minute hands with 
    # reference to 12:00
    hour_angle = 0.5 * (hh * 60 + mm)
    minute_angle = 6 * mm
  
    # Find the difference between
    # two angles
    angle = abs(hour_angle - minute_angle)
  
    # Return the smaller angle of two
    # possible angles
    angle = min(360 - angle, angle)
  
    return angle
  
# function to print all time when
# angle between hour hand and minute
# hand is theta
def printTime(theta):
  
    for hh in range(0, 12):
        for mm in range(0, 60):
            if (calcAngle(hh, mm)==theta):
                print(hh, ":", mm, sep = "")
                return
              
    print("Input angle not valid.")
    return
  
# driver code to test ablove function
theta = 90.0
printTime(theta)
  
# This code is contributed by Smitha

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find time for a given
// angle.
using System;
  
class GFG {
  
    // function to find angle between
    // hour hand and minute hand
    static float calcAngle(int hh, int mm)
    {
          
        // Calculate the angles moved by hour
        // and minute hands with reference 
        // to 12:00
        float hour_angle = 0.5f * (hh * 60 + mm);
        float minute_angle = 6 * mm;
      
        // Find the difference between two angles
        float angle = Math.Abs(hour_angle
                                 - minute_angle);
      
        // Return the smaller angle of 
        // two possible angles
        angle = Math.Min(360 - angle, angle);
      
        return angle;
    }
      
    // function to print all time when 
    // angle between hour hand and minute
    // hand is theta
    static void printTime(float theta)
    {
        for (int hh = 0; hh < 12; hh++)
        {
            for (int mm = 0; mm < 60; mm++)
            {
                if (calcAngle(hh, mm) == theta)
                {
                    Console.WriteLine(hh + ":" + mm);
                    return;
                }
            }
        }
          
        Console.WriteLine("Input angle not valid.");
        return;
    
      
    // Driver code 
    public static void Main ()
    {
        float theta = 90.0f;
          
        printTime(theta);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find 
// time for a given angle.
  
// Function to find angle between
// hour hand and minute hand
function calcAngle($hh, $mm)
{
    // Calculate the angles
    // moved by hour and minute 
    // hands with reference to 12:00
    $hour_angle = 0.5 * ($hh * 60 + $mm);
    $minute_angle = 6 * $mm;
  
    // Find the difference
    // between two angles
    $angle = abs($hour_angle
                 $minute_angle);
  
    // Return the smaller angle 
    // of two possible angles
    $angle = min(360 - $angle, $angle);
  
    return $angle;
}
  
// function to print all
// time when angle between
// hour hand and minute
// hand is theta
function printTime( $theta)
{
    for ($hh = 0; $hh < 12; $hh++)
    {
        for ($mm = 0; $mm < 60; $mm++)
        {
            if (calcAngle($hh, $mm) == $theta)
            {
                echo $hh, ":", $mm;
                return;
            }
        }
    }
    echo "Input angle not valid.\n";
    return;
}
  
// Driver Code
$theta = 90.0;
printTime($theta);
  
// This code is contributed by anuj_67.
?>

chevron_right



Output :

3:0

Time Complexity : O(1)
Auxiliary Space : O(1)

This article is contributed by Pratik Chhajer. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Smitha Dinesh Semwal, vt_m



Article Tags :

Be the First to upvote.


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