LeetCode Problem Workspace
Count Pairs That Form a Complete Day I
Count all pairs in an array where their sum forms a complete day using hash-based counting for efficiency.
3
Topics
5
Code langs
3
Related
Practice Focus
Easy · Array scanning plus hash lookup
Answer-first summary
Count all pairs in an array where their sum forms a complete day using hash-based counting for efficiency.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array scanning plus hash lookup
Start by scanning the array while tracking remainders modulo 24 in a hash map. For each number, check how many previous numbers complement it to a full day. This method avoids brute force by counting compatible pairs directly, ensuring linear performance with respect to array length.
Problem Statement
Given an integer array hours, return the number of index pairs (i, j) where i < j and the sum hours[i] + hours[j] is a multiple of 24. Each pair represents a combination of work hours that completes a full day.
A complete day is any sum of hours divisible evenly by 24. For example, 24 hours, 48 hours, or 72 hours are considered complete days, and your task is to count all valid pairs forming these totals.
Examples
Example 1
Input: hours = [12,12,30,24,24]
Output: 2
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
The pairs of indices that form a complete day are (0, 1) , (0, 2) , and (1, 2) .
Constraints
- 1 <= hours.length <= 100
- 1 <= hours[i] <= 109
Solution Approach
Hash remainder counting
Use a hash map to count occurrences of hours modulo 24. For each number, calculate its remainder and find how many previous numbers have a complementary remainder to sum to 24.
Single pass accumulation
Iterate through the array once, updating the hash map of remainders as you go. Each time you encounter a number, add the count of matching remainders to the total pairs count.
Avoid double counting
Ensure that you only consider pairs where the first index is less than the second to prevent counting the same pair twice. Use the hash map to track prior numbers efficiently.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(n) due to a single pass over the array with constant-time hash operations. Space complexity is O(24) = O(1) for the remainder hash map since there are only 24 possible remainders.
What Interviewers Usually Probe
- Asks about handling large numbers or values exceeding 24 hours.
- Checks if you can optimize from brute force to a hash-based approach.
- Wants clarity on handling modulo arithmetic and avoiding double counting.
Common Pitfalls or Variants
Common pitfalls
- Brute force all pairs instead of using modulo hash counting.
- Forgetting to handle the remainder 0 correctly, which forms complete days with itself.
- Double counting pairs by not ensuring i < j.
Follow-up variants
- Count pairs that sum to a multiple of k instead of 24, changing the modulo base.
- Return the list of pairs themselves instead of just the count, requiring more storage.
- Handle negative or zero hours while still using the modulo hash approach.
FAQ
How do I count pairs that form a complete day efficiently?
Track remainders modulo 24 in a hash map while scanning the array and count complementary remainders to form full days.
Why does the problem use hours[i] + hours[j] % 24?
Using modulo 24 identifies sums that are exact multiples of a day without computing large totals, keeping operations efficient.
Can GhostInterview handle arrays with more than 100 elements?
Yes, the hash map approach scales linearly with array size, so it works efficiently even near the upper limit of 100 elements.
What if the array has repeated values like multiple 24s?
The hash map automatically counts occurrences, so repeated values correctly contribute to multiple valid pairs without extra effort.
Is this problem an example of array scanning plus hash lookup?
Exactly, it demonstrates scanning the array while using a hash map to track and count complementary remainders for a target sum pattern.
Solution
Solution 1: Counting
We can use a hash table or an array $\textit{cnt}$ of length $24$ to record the occurrence count of each hour modulo $24$.
class Solution:
def countCompleteDayPairs(self, hours: List[int]) -> int:
cnt = Counter()
ans = 0
for x in hours:
ans += cnt[(24 - (x % 24)) % 24]
cnt[x % 24] += 1
return ansContinue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array scanning plus hash lookup
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward