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.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 5 * 1051 <= 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]
pairs-of-songs-with-total-durations-divisible-by-60)check-if-array-pairs-are-divisible-by-k)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3185: Count Pairs That Form a Complete Day II
class Solution {
public long countCompleteDayPairs(int[] hours) {
int[] cnt = new int[24];
long ans = 0;
for (int x : hours) {
ans += cnt[(24 - x % 24) % 24];
++cnt[x % 24];
}
return ans;
}
}
// Accepted solution for LeetCode #3185: Count Pairs That Form a Complete Day II
func countCompleteDayPairs(hours []int) (ans int64) {
cnt := [24]int{}
for _, x := range hours {
ans += int64(cnt[(24-x%24)%24])
cnt[x%24]++
}
return
}
# Accepted solution for LeetCode #3185: Count Pairs That Form a Complete Day II
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 #3185: Count Pairs That Form a Complete Day II
/**
* [3185] Count Pairs That Form a Complete Day II
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn count_complete_day_pairs(hours: Vec<i32>) -> i64 {
use std::collections::HashMap;
let mut map = HashMap::new();
let mut result = 0;
for hour in hours {
let hour = hour % 24;
if let Some(&c) = map.get(&((24 - hour) % 24)) {
result += c;
}
let entry = map.entry(hour).or_insert(0);
*entry += 1;
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3185() {
assert_eq!(
2,
Solution::count_complete_day_pairs(vec![12, 12, 30, 24, 24])
);
assert_eq!(3, Solution::count_complete_day_pairs(vec![72, 48, 24, 3]));
}
}
// Accepted solution for LeetCode #3185: Count Pairs That Form a Complete Day II
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.