Given an integer array hours representing times in hours, return an integer denoting the number of pairs i, j where i < j and hours[i] + hours[j] forms a complete day.
A complete day is defined as a time duration that is an exact multiple of 24 hours.
For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.
Example 1:
Input: hours = [12,12,30,24,24]
Output: 2
Explanation: The pairs of indices that form a complete day are (0, 1) and (3, 4).
Example 2:
Input: hours = [72,48,24,3]
Output: 3
Explanation: The pairs of indices that form a complete day are (0, 1), (0, 2), and (1, 2).
Constraints:
1 <= hours.length <= 5 * 1051 <= hours[i] <= 109This is a straightforward approach where you iterate over all possible pairs in the array and check if their sum is a multiple of 24. Although not optimal, it's the simplest solution to understand the problem and attempt manual calculations for small inputs.
In this C code, we define a function countCompleteDays which processes all possible pairs using nested loops, checking if their sum is divisible by 24 (i.e., (hours[i] + hours[j]) % 24 == 0). The time complexity is O(n^2) since we are checking each pair, and the space complexity is O(1) because we are not using additional storage proportional to the input size.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2)
Space Complexity: O(1)
This approach optimizes the search for pairs whose sums are multiples of 24 by using a hash map (or dictionary) to track previously seen remainders when each hour is divided by 24. This reduces time complexity to linear.
This C code uses an array remainderCount of size 24 to keep track of the frequency of remainders when elements of hours are divided by 24. It efficiently counts pairs forming complete days by using these remainders.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n)
Space Complexity: O(1)
| Approach | Complexity |
|---|---|
| Brute Force Approach | Time Complexity: O(n^2) |
| Optimized Approach Using Hash Map | Time Complexity: O(n) |
I solved too many Leetcode problems • NeetCodeIO • 101,495 views views
Watch 9 more video solutions →Practice Count Pairs That Form a Complete Day II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor