Sponsored
Sponsored
This approach involves calculating the angles made by the hour hand and the minute hand with respect to 12:00 and then finding the smallest angle between them. The minute hand moves 6 degrees per minute (360 degrees / 60 minutes), and the hour hand moves 30 degrees per hour (360 degrees / 12 hours) plus an additional 0.5 degrees per minute. The difference between these angles gives us the desired result.
Time Complexity: O(1) - The computation involves a fixed number of arithmetic operations.
Space Complexity: O(1) - No additional space is required beyond fixed-size variables.
1#include <stdio.h>
2#include <math.h>
3
4float angleClock(int hour, int minutes) {
5 double minute_angle = minutes * 6.0;
6 double hour_angle = (hour % 12) * 30.0 + minutes * 0.5;
7 double angle = fabs(minute_angle - hour_angle);
8 return (angle > 180) ? 360 - angle : angle;
9}
10
11int main() {
12 printf("%.5f\n", angleClock(12, 30)); // Output: 165.00000
13 printf("%.5f\n", angleClock(3, 30)); // Output: 75.00000
14 printf("%.5f\n", angleClock(3, 15)); // Output: 7.50000
15 return 0;
16}
The C program computes angles for the hour and minute hands separately. The minute hand covers 6 degrees per minute. The hour hand moves 30 degrees per hour and 0.5 degrees per minute. The difference between these angles is calculated, and the smaller angle is determined by comparing with 180 degrees. The main function demonstrates usage with example times.
This alternative approach converts the movement of the clock hands into their equivalent rotations, effectively translating this into angles. The goal is to determine the position of both hands as angles relative to the 12 o'clock position and compute the minimal angular difference.
Time Complexity: O(1) - Fixed-time operations based on input.
Space Complexity: O(1) - Uses a constant number of variables.
1
Python's version condenses tasks similarly, utilizing the totality of elapsed minutes to determine rotational alignments for both clock hands, generating the resultant minimal deviation in their absolute displacement.