Watch 10 video solutions for Count Pairs That Form a Complete Day I, a easy level problem involving Array, Hash Table, Counting. This walkthrough by Aryan Mittal has 3,738 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 1001 <= hours[i] <= 109Problem Overview: You get an array hours where each value represents time spent on a task. The goal is to count pairs (i, j) such that hours[i] + hours[j] equals a multiple of 24. In other words, the total hours of the pair form a complete day.
Approach 1: Brute Force Pair Checking (O(n2) time, O(1) space)
The most direct approach checks every possible pair of indices. Use two nested loops: the outer loop picks the first element, and the inner loop checks all elements after it. For each pair, compute (hours[i] + hours[j]) % 24. If the result equals 0, the pair forms a complete day and you increment the count. This approach uses constant extra memory because it only tracks the pair count. It works fine for small inputs but becomes inefficient as n grows since it performs roughly n * (n-1) / 2 comparisons.
Approach 2: Remainder Counting with Hash Table (O(n) time, O(1) space)
A faster solution uses modular arithmetic. Instead of comparing raw values, reduce each hour using remainder = hours[i] % 24. Two numbers form a complete day when their remainders add up to 24, or when both are 0. Maintain a frequency map (or fixed array of size 24) storing how many times each remainder has appeared. For every new value, compute its complement (24 - remainder) % 24. If that complement remainder was seen before, those previous elements form valid pairs with the current element. Add that frequency to the answer, then record the current remainder.
This turns pair detection into a constant-time lookup. Each element is processed once, giving O(n) time complexity. The extra memory stays constant because only 24 remainder buckets are stored. The idea is similar to classic problems like Two Sum but applied to modular arithmetic using a hash table or frequency array.
Since the input is just a list of numbers, iteration over the array combined with remainder frequency counting makes the solution both simple and efficient.
Recommended for interviews: Interviewers typically expect the remainder counting approach. Starting with the brute force method shows you understand the pair requirement, but recognizing the % 24 pattern and converting the problem into remainder complements demonstrates stronger algorithmic thinking and reduces the complexity from O(n2) to O(n).
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Checking | O(n²) | O(1) | Good for understanding the problem or when input size is very small. |
| Remainder Counting with Hash Table | O(n) | O(1) | Best general solution. Efficient for large arrays and commonly expected in interviews. |