Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[12,12,30,24,24]
[72,48,24,3]
check-if-array-pairs-are-divisible-by-k)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3184: Count Pairs That Form a Complete Day I
class Solution {
public int countCompleteDayPairs(int[] hours) {
int[] cnt = new int[24];
int ans = 0;
for (int x : hours) {
ans += cnt[(24 - x % 24) % 24];
++cnt[x % 24];
}
return ans;
}
}
// Accepted solution for LeetCode #3184: Count Pairs That Form a Complete Day I
func countCompleteDayPairs(hours []int) (ans int) {
cnt := [24]int{}
for _, x := range hours {
ans += cnt[(24-x%24)%24]
cnt[x%24]++
}
return
}
# Accepted solution for LeetCode #3184: Count Pairs That Form a Complete Day I
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 ans
// Accepted solution for LeetCode #3184: Count Pairs That Form a Complete Day I
fn count_complete_day_pairs(hours: Vec<i32>) -> i32 {
let mut ret = 0;
let len = hours.len();
for i in 0..len {
for j in (i + 1)..len {
if (hours[i] + hours[j]) % 24 == 0 {
ret += 1;
}
}
}
ret
}
fn main() {
let hours = vec![12, 12, 30, 24, 24];
let ret = count_complete_day_pairs(hours);
println!("ret={ret}");
}
#[test]
fn test() {
{
let hours = vec![12, 12, 30, 24, 24];
let ret = count_complete_day_pairs(hours);
assert_eq!(ret, 2);
}
{
let hours = vec![72, 48, 24, 3];
let ret = count_complete_day_pairs(hours);
assert_eq!(ret, 3);
}
}
// Accepted solution for LeetCode #3184: Count Pairs That Form a Complete Day I
function countCompleteDayPairs(hours: number[]): number {
const cnt: number[] = Array(24).fill(0);
let ans: number = 0;
for (const x of hours) {
ans += cnt[(24 - (x % 24)) % 24];
++cnt[x % 24];
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.