top of page

TCS Codevita | Clock Angle

In this blog, we will discuss another Problem asked in TCS Codevita 2019.

Before running through the solution I would recommend to solve it by yourself first.


Problem Description:


There are 360 Longitudes on the Earth, which are equidistant vertical imaginary lines drawn on the Earth, separated by 1 degree each from the center of the Earth. The period of the rotation of the Earth on its axis is 24 hours. All countries have their own official times and hence time zones.


UTC is a universal time coordinate that passes through 0 (Zero degrees) longitude.


Time at a particular location on Earth can be calculated using the period of the rotation of Earth and the longitude of that particular location. For example, Indian time zone IST (Indian Standard Time) is located at 82.5° E longitude. Hence, Indian time can be calculated as below:-


IST = UTC + (24/360)*82.5 = UTC + 5:30Hrs


Now suppose we changed the period of rotation of the earth using some imaginary power, this will change the time at every longitude on the earth.


Calculate the smallest angle between the hour and the minute hand of the clock, which shows the difference of time at a particular longitude and the time at UTC i.e. we have to take smaller of the two angles formed between an hour and minute hand.



Constraints:

To show the time difference on the clock, a 12-hour clock (as shown below) shall be used, irrespective of the period of the earth's rotation, for this question only.


Input Format:

1. Period of the earth’s rotation in Hours (Integer only)


2. Value of Longitude up to 2 places of decimal


Output:

The smallest angle between the hour and the minute hand of the clock, which shows the difference between the time at a particular longitude and time at UTC, up to 2 decimal places.


Explanation:

Example 1:


Input:

24
82.50

Output:

15.00

Explanation:


If the period of rotation of the earth is 24 hours then time at 82.5-degree longitude will be (24/360)*82.50 = 5:30 and the minimum angle at this time between the minute and hour hand will be 15 degrees.


Example 2:


Input:

12
360.00

Output:

0.00

Explanation:


If the period of rotation of the earth is 12 hours then time at 360-degree longitude will be (12/360)*360 = 12:00 and the minimum angle at this time between the minute and hour hand will be 0 degrees.

 

Implementation Using C++:

#include<iostream>
#include<cstdlib>

using namespace std;

int min(int x, int y)
{
    return (x < y)? x : y;
}

int angleDifference(int hour, float minute)
{
    float time;
    float fhour = (float)hour; //typecasting int into float
    time = ((float)(fhour/360)) * minute;
    
    int htime = (int)time;
    float a = (float)time - htime;
    float b = a * 0.6;
    int mtime = b * 1005;
    
    if(htime == 12)
    {
        htime = 0;
    }
    
    if(mtime == 60)
    {
        mtime = 0;
    }
    
    //hrs converted to minutes
    int hour_angle = 0.5 * (htime*60 + mtime);
    
    int minute_angle = 6 * mtime;
     
    int angle = abs(hour_angle - minute_angle); 
    return angle;
}

int main(void)
{
    int earth_rota;
    float longi;
    
    scanf("%d", &earth_rota);
    scanf("%f", &longi);
    
    float angle;
    angle = angleDifference(earth_rota, longi);
    
    printf("%.2f", angle);
    
    return 0;
}
 

Happy Coding!

Follow us on Instagram @programmersdoor

Join us on Telegram @programmersdoor


Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.

Follow Programmers Door for more.

676 views0 comments

Recent Posts

See All
bottom of page