Count Pairs That Form a Complete Day II - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: hours = [12,12,30,24,24]
Output: 2
💡 Note: The valid pairs are (0,1) where 12+12=24 and (3,4) where 24+24=48. Both sums are multiples of 24.
Example 2 — Mixed Remainders
$ Input: hours = [72,48,24,3]
Output: 3
💡 Note: Valid pairs: (0,3) gives 72+3=75=24×3+3, wait that's wrong. Let me recalculate: 72%24=0, 48%24=0, 24%24=0, 3%24=3. We need pairs that sum to multiple of 24. Actually (0,1): 72+48=120=24×5, (0,2): 72+24=96=24×4, (1,2): 48+24=72=24×3. So answer is 3.
Example 3 — No Valid Pairs
$ Input: hours = [1,2,3,4]
Output: 0
💡 Note: No pair sums to a multiple of 24: 1+2=3, 1+3=4, 1+4=5, 2+3=5, 2+4=6, 3+4=7. None are divisible by 24.

Constraints

  • 1 ≤ hours.length ≤ 5 × 105
  • 1 ≤ hours[i] ≤ 109

Visualization

Tap to expand
Count Pairs That Form Complete DaysInput: [12, 12, 30, 18, 6]12123018612+12=24✓30+18=48✓18+6=24✓Complete Days Found:• 1 day (24 hours)• 2 days (48 hours)• 1 day (24 hours)Output: 3 pairs form complete days
Understanding the Visualization
1
Input Hours
Array of hours: [12, 12, 30, 18, 6]
2
Find Pairs
Check which pairs sum to multiples of 24
3
Count Result
Return total number of valid pairs
Key Takeaway
🎯 Key Insight: Two hours form a complete day if their sum is divisible by 24
Asked in
Amazon 15 Microsoft 12 Google 8
23.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