#1010
Medium Algorithms Pairs of songs with total durations divisible by 60
Array Hash Table Counting
53.4% acceptance
Feb 25, 2026
4326
182
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn num_pairs_divisible_by60(time: Vec<i32>) -> i32 {
let mut cnt = [0i32; 60];
let mut ans = 0;
for t in time {
let r = (t % 60) as usize;
ans += cnt[(60 - r) % 60];
cnt[r] += 1;
}
ans
}
}