Prepin
Log in

Leetcode 1010. Song Pairs Whose Total Length is Divisible by 60

Approach / Explanation

This question presents you with a collection of song durations measured in seconds. Your task is to determine how many pairs of songs have a combined duration that is divisible by 60 seconds. The objective is to compute the number of pairs that yield a total duration divisible by 60. Specifically, it asks you to count the number of pairs (a[i], a[j]) from the array a[] such that the sum a[i] + a[j] is divisible by 60. To solve this problem, one effective method is to utilize a hash map data structure. Here are some important steps to keep in mind: Create an array called record with a size of 60 to track the remainders obtained when each number in the list is divided by 60. For every number in the list, update the corresponding index in the record. For each number in the list, verify if there is another number in the array that can combine with it to form a sum divisible by 60.

Suggested Answer

Imagine you have a list [30,20,150,100,40] . First, we will create an array called record[60] to keep track of the remainder of each number in the list when divided by 60. This array will look like [0,0,0,0,0,2,0,...,1,0,...,1,0,0,0] . Next, we will revisit the list. For each number list[i], we need to determine if there is another number that, when added to it, results in a sum divisible by 60. For instance, for the number 30, we check against the number 30 (since 60 - 30 % 60 = 30). In the record array, record[30] equals 1. This indicates that there is one pair of numbers whose sum is divisible by 60. Lastly, by summing the number of pairs for each entry in the list, we find that there are 3 pairs in total. Thus, the overall number of song pairs with durations that total to a multiple of 60 is 3.

Alternative Answer

Consider an array [60,60,60] . First, you initialize the array record[60], resulting in a count of [3,0,...,0] after processing the input array. Next, for each number in the array, determine if there exists another number that, when added together, yields a sum that is divisible by 60. For example, with the number 60, the corresponding number to find is 0 (since 60 - 60 % 60 = 0). In the record array, record[0] equals 3. This indicates that there are a total of 3 pairs of numbers. Therefore, the overall number of pairs of songs with total durations that are divisible by 60 is 3.

Question Details

Difficulty & Category

Medium
algorithms

Companies

JP Morgan Chase

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.