Count Pairs That Form a Complete Day I - Problem
You're given an array of hours representing different time durations. Your task is to find how many pairs of these hours can be combined to form a complete day (exactly 24 hours or any multiple of 24).
A complete day means the sum of two hours equals 24k where k is any positive integer:
- 1 day = 24 hours
- 2 days = 48 hours
- 3 days = 72 hours
- and so on...
Important: You can only count pairs where i < j (avoid counting the same pair twice).
Example: If hours = [12, 12, 30, 24, 24], the pairs (0,1), (3,4) form complete days: 12+12=24 and 24+24=48.
Input & Output
example_1.py — Basic case
$
Input:
hours = [12, 12, 30, 24, 24]
›
Output:
2
💡 Note:
The pairs (0,1) and (3,4) form complete days: 12+12=24 and 24+24=48. Both are multiples of 24.
example_2.py — No valid pairs
$
Input:
hours = [72, 48, 24, 3]
›
Output:
3
💡 Note:
Valid pairs are (0,3): 72+3=75→no, (1,3): 48+3=51→no, (2,3): 24+3=27→no, (0,1): 72+48=120→yes, (0,2): 72+24=96→yes, (1,2): 48+24=72→yes. So 3 pairs total.
example_3.py — Single element
$
Input:
hours = [1]
›
Output:
0
💡 Note:
With only one element, no pairs can be formed, so the result is 0.
Visualization
Tap to expand
Understanding the Visualization
1
Shape Classification
Each hour gets a shape based on hour % 24 (0-23 possible shapes)
2
Complement Search
For each piece, we look for pieces with complementary shapes
3
Perfect Matches
Shape A + Shape B = 24 means they form a complete circle
4
Count & Update
Count existing matches, then add current piece to collection
Key Takeaway
🎯 Key Insight: The magic of modular arithmetic transforms a complex pairing problem into simple complement counting. Each remainder from 0-23 has exactly one complement that forms a complete day!
Time & Space Complexity
Time Complexity
O(n)
Single pass through array, each operation is O(1) with hash map
✓ Linear Growth
Space Complexity
O(1)
Hash map stores at most 24 different remainders (0 to 23), which is constant space
✓ Linear Space
Constraints
- 1 ≤ hours.length ≤ 100
- 1 ≤ hours[i] ≤ 109
- All pairs must satisfy i < j condition
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code