
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Pairs of Songs With Total Durations Divisible by 60 in Python
Suppose we have a list of songs, the i-th song has a duration of time[i] seconds. We have to find the number of pairs of songs for which their total time in seconds is divisible by 60.
So if the time array is like [30, 20, 150, 100, 40], then the answer will be 3. Three pairs will be (3, 150), (20, 100), (20, 40) for all cases the total duration is divisible by 60.
To solve this, we will follow these steps −
- Take a map rem to store remainders. Set ans := 0
- for all elements i in time −
- if i is divisible by 0 and 0 in rem, then ans := ans + rem[0]
- o otherwise when 60 – (i mod 60) in rem, then ans := ans + rem[60 – (i mod 60)]
- if i mod 60 in rem, then rem[i mod 60] := rem[i mod 60] + 1
- otherwise rem[i mod 60] := 1
- return the ans
Example
Let us see the following implementation to get better understanding −
class Solution(object): def numPairsDivisibleBy60(self, time): ans = 0 remainder = {} for i in time: if i % 60 == 0 and 0 in remainder: ans += remainder[0] elif 60 - (i%60) in remainder: ans += remainder[60 - (i%60)] if i % 60 in remainder: remainder[i%60]+=1 else: remainder[i%60]=1 return ans ob1 = Solution() print(ob1.numPairsDivisibleBy60([30,20,150,100,40]))
Input
[30,20,150,100,40]
Output
3
- Related Articles
- Python - Total equal pairs in List
- Count pairs of numbers from 1 to N with Product divisible by their Sum in C++
- Program to check if array pairs are divisible by k or not using Python
- Maximize the number of sum pairs which are divisible by K in C++
- Count pairs in array whose sum is divisible by K in C++
- Count pairs in array whose sum is divisible by 4 in C++
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- Find the total two-digit numbers which are divisible by $5$.
- Binary Prefix Divisible By 5 in Python
- Smallest Integer Divisible by K in Python
- Count divisible pairs in an array in C++
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Sort Tuples by Total digits in Python

Advertisements