3185. Count Pairs That Form a Complete Day II

3185. Count Pairs That Form a Complete Day II

Description

Given an integer array hours representing times in hours , return an integer denoting the number of pairs i, j where i < j and hours[i] + hours[j] forms a complete day .

A complete day is defined as a time duration that is an exact multiple of 24 hours.

For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.

Example 1:

1
2
3
4
5
Input: hours = [12,12,30,24,24]

Output: 2

Explanation: The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`.

Example 2:

1
2
3
4
5
Input: hours = [72,48,24,3]

Output: 3

Explanation: The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`.

Constraints:

  • 1 <= hours.length <= 5 * 10^5
  • 1 <= hours[i] <= 10^9

Hints/Notes

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
long long countCompleteDayPairs(vector<int>& hours) {
long long res = 0;
map<int, long long> m;
for (int hour: hours) {
int h = (24 - hour % 24) % 24;
res += m[h];
m[hour % 24]++;
}
return res;
}
};