1010. Pairs of Songs With Total Durations Divisible by 60 LeetCode Solution

In this guide, you will get 1010. Pairs of Songs With Total Durations Divisible by 60 LeetCode Solution with the best time and space complexity. The solution to Pairs of Songs With Total Durations Divisible by problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Pairs of Songs With Total Durations Divisible by solution in C++
  4. Pairs of Songs With Total Durations Divisible by solution in Java
  5. Pairs of Songs With Total Durations Divisible by solution in Python
  6. Additional Resources
1010. Pairs of Songs With Total Durations Divisible by 60 LeetCode Solution image

Problem Statement of Pairs of Songs With Total Durations Divisible by

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 * 104
1 <= time[i] <= 500

Complexity Analysis

  • Time Complexity:
  • Space Complexity:
See also  38. Count and Say LeetCode Solution

1010. Pairs of Songs With Total Durations Divisible by 60 LeetCode Solution in C++

class Solution {
 public:
  int numPairsDivisibleBy60(vector<int>& time) {
    int ans = 0;
    vector<int> count(60);

    for (int t : time) {
      t %= 60;
      ans += count[(60 - t) % 60];
      ++count[t];
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1010. Pairs of Songs With Total Durations Divisible by 60 LeetCode Solution in Java

class Solution {
  public int numPairsDivisibleBy60(int[] time) {
    int ans = 0;
    int[] count = new int[60];

    for (int t : time) {
      t %= 60;
      ans += count[(60 - t) % 60];
      ++count[t];
    }

    return ans;
  }
}
// code provided by PROGIEZ

1010. Pairs of Songs With Total Durations Divisible by 60 LeetCode Solution in Python

class Solution:
  def numPairsDivisibleBy60(self, time: list[int]) -> int:
    ans = 0
    count = [0] * 60

    for t in time:
      t %= 60
      ans += count[(60 - t) % 60]
      count[t] += 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.