3185. Count Pairs That Form a Complete Day II LeetCode Solution
In this guide, you will get 3185. Count Pairs That Form a Complete Day II LeetCode Solution with the best time and space complexity. The solution to Count Pairs That Form a Complete Day II 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
- Problem Statement
- Complexity Analysis
- Count Pairs That Form a Complete Day II solution in C++
- Count Pairs That Form a Complete Day II solution in Java
- Count Pairs That Form a Complete Day II solution in Python
- Additional Resources
Problem Statement of Count Pairs That Form a Complete Day II
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:
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:
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 * 105
1 <= hours[i] <= 109
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(24) = O(1)
3185. Count Pairs That Form a Complete Day II LeetCode Solution in C++
class Solution {
public:
// Same as 3184. Count Pairs That Form a Complete Day I
long long countCompleteDayPairs(vector<int>& hours) {
long ans = 0;
vector<int> count(24);
for (const int hour : hours) {
ans += count[(24 - hour % 24) % 24];
++count[hour % 24];
}
return ans;
}
};
/* code provided by PROGIEZ */
3185. Count Pairs That Form a Complete Day II LeetCode Solution in Java
class Solution {
// Same as 3184. Count Pairs That Form a Complete Day I
public long countCompleteDayPairs(int[] hours) {
long ans = 0;
int[] count = new int[24];
for (final int hour : hours) {
ans += count[(24 - hour % 24) % 24];
++count[hour % 24];
}
return ans;
}
}
// code provided by PROGIEZ
3185. Count Pairs That Form a Complete Day II LeetCode Solution in Python
class Solution:
# Same as 3184. Count Pairs That Form a Complete Day I
def countCompleteDayPairs(self, hours: list[int]) -> int:
ans = 0
count = [0] * 24
for hour in hours:
ans += count[(24 - hour % 24) % 24]
count[hour % 24] += 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.