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
🧩 The Complete Day PuzzleHours: [12, 12, 30, 24]Step 1: Convert to puzzle pieces (remainders)12rem: 1212rem: 1230rem: 624rem: 0Complement Logic:rem=12 needs rem=12 → 12+12=24 ✓rem=6 needs rem=18 → 6+18=24rem=0 needs rem=0 → 0+0=0 ✓24Complete Day!🎯 Key InsightTwo puzzle pieces fit together when: remainder₁ + remainder₂ ≡ 0 (mod 24)This happens when remainders are (0,0), (1,23), (2,22), ..., (12,12), ..., (23,1)Hash table tracks piece inventory - O(n) time, O(1) space!
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

n
2n
Linear Growth
Space Complexity
O(1)

Hash map stores at most 24 different remainders (0 to 23), which is constant space

n
2n
Linear Space

Constraints

  • 1 ≤ hours.length ≤ 100
  • 1 ≤ hours[i] ≤ 109
  • All pairs must satisfy i < j condition
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
26.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen