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.
You are given a list of songs where the ith song has a duration of time[i] seconds.
Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0.
Example 1:
Input: time = [30,20,150,100,40] Output: 3 Explanation: Three pairs have a total duration divisible by 60: (time[0] = 30, time[2] = 150): total duration 180 (time[1] = 20, time[3] = 100): total duration 120 (time[1] = 20, time[4] = 40): total duration 60
Example 2:
Input: time = [60,60,60] Output: 3 Explanation: All three pairs have a total duration of 120, which is divisible by 60.
Constraints:
1 <= time.length <= 6 * 1041 <= time[i] <= 500Problem summary: You are given a list of songs where the ith song has a duration of time[i] seconds. Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[30,20,150,100,40]
[60,60,60]
destroy-sequential-targets)count-pairs-that-form-a-complete-day-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1010: Pairs of Songs With Total Durations Divisible by 60
class Solution {
public int numPairsDivisibleBy60(int[] time) {
int[] cnt = new int[60];
for (int t : time) {
++cnt[t % 60];
}
int ans = 0;
for (int x = 1; x < 30; ++x) {
ans += cnt[x] * cnt[60 - x];
}
ans += (long) cnt[0] * (cnt[0] - 1) / 2;
ans += (long) cnt[30] * (cnt[30] - 1) / 2;
return ans;
}
}
// Accepted solution for LeetCode #1010: Pairs of Songs With Total Durations Divisible by 60
func numPairsDivisibleBy60(time []int) (ans int) {
cnt := [60]int{}
for _, t := range time {
cnt[t%60]++
}
for x := 1; x < 30; x++ {
ans += cnt[x] * cnt[60-x]
}
ans += cnt[0] * (cnt[0] - 1) / 2
ans += cnt[30] * (cnt[30] - 1) / 2
return
}
# Accepted solution for LeetCode #1010: Pairs of Songs With Total Durations Divisible by 60
class Solution:
def numPairsDivisibleBy60(self, time: List[int]) -> int:
cnt = Counter(t % 60 for t in time)
ans = sum(cnt[x] * cnt[60 - x] for x in range(1, 30))
ans += cnt[0] * (cnt[0] - 1) // 2
ans += cnt[30] * (cnt[30] - 1) // 2
return ans
// Accepted solution for LeetCode #1010: Pairs of Songs With Total Durations Divisible by 60
struct Solution;
impl Solution {
fn num_pairs_divisible_by60(time: Vec<i32>) -> i32 {
let mut a: Vec<i32> = vec![0; 60];
let mut res = 0;
for x in time {
let count = a[((600 - x) % 60) as usize];
if count != 0 {
res += count;
}
a[(x % 60) as usize] += 1;
}
res
}
}
#[test]
fn test() {
let time = vec![30, 20, 150, 100, 40];
assert_eq!(Solution::num_pairs_divisible_by60(time), 3);
let time = vec![60, 60, 60];
assert_eq!(Solution::num_pairs_divisible_by60(time), 3);
}
// Accepted solution for LeetCode #1010: Pairs of Songs With Total Durations Divisible by 60
function numPairsDivisibleBy60(time: number[]): number {
const cnt: number[] = new Array(60).fill(0);
for (const t of time) {
++cnt[t % 60];
}
let ans = 0;
for (let x = 1; x < 30; ++x) {
ans += cnt[x] * cnt[60 - x];
}
ans += (cnt[0] * (cnt[0] - 1)) / 2;
ans += (cnt[30] * (cnt[30] - 1)) / 2;
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.