Pairs of Songs With Total Durations Divisible by 60

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 whose total duration in seconds is divisible by 60. Formally, return the number of indices i, j such that i < j and (time[i] + time[j]) % 60 == 0.

30:00

Pairs of Songs With Total Durations Divisible by 60
medium
Free written solution
Topics
Companies

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 whose total duration in seconds is divisible by 60. Formally, return the number of indices i, j such that i < j and (time[i] + time[j]) % 60 == 0.

Example 1:
Input: {"time":[30,20,150,100,40]}
Output: 3
Constraints:
  • 1≤time.length≤6×1041 \leq \text{time.length} \leq 6 \times 10^4

  • 1≤time[i]≤5001 \leq \text{time}[i] \leq 500

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"time":[30,20,150,100,40]}

Only t % 60 matters. Put each of the 5 songs on a 60-slot dial by its remainder; a song pairs with every earlier song sitting on the mirrored slot.

30s

%30

#0

20s

%20

#1

150s

%30

#2

100s

%40

#3

40s

%40

#4

0153045pairs0

current slot r

-

partner slot (60 - r) % 60

-

this song adds

-

Slots r and 60 - r mirror each other across the dashed line; 0 and 30 pair with themselves.

Variables
VariableValue
pairs0
DepthFunction Call
Stack empty
0/11