The Indian Engineer

Problem 3185 Count Pairs That Form a Complete Day II

Posted on 6 mins

Array Hash-Table Counting

Problem Statement

Link - Problem 3185

Question

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:

Solution

class Solution {
public:
    long long countCompleteDayPairs(vector<int>& hours) {
        unordered_map<int, int> seenMap;
        long long count = 0;

        for (int hour : hours) {
            int rem = hour % 24;
            int complement = (24 - rem) % 24;
            if (seenMap.find(complement) != seenMap.end()) {
                count += seenMap[complement];
            }
            seenMap[rem]++;
        }

        return count;
    }
};

Complexity Analysis

| Algorithm                   | Time Complexity | Space Complexity |
| --------------------------- | --------------- | ---------------- |
| Hash Table Complement Count | O(n)            | O(1)             |

Explanation

Intial Thoughts

To solve this problem, we need to identify pairs of hours that sum up to a complete day, which is a multiple of 24 hours. We can start by examining each hour in the array and checking if its complement, or the number of hours needed to complete a day, exists in the array. We can use the modulo operator to find the remainder of each hour when divided by 24. This will help us find the complements more efficiently. We can also use a data structure like a hashmap to store the hours we have seen so far and their frequencies. This will allow us to quickly look up the complements and count the pairs. Initially, we should focus on understanding the properties of complete days and how to calculate the complements of each hour.

Intuitive Analysis

Intuitively, we can think of this problem as finding pairs of puzzle pieces that fit together to form a complete picture, which is a complete day in this case. We can start by grouping the hours into categories based on their remainders when divided by 24. This will help us identify the potential complements and find the pairs more efficiently. We can also use the fact that if a pair of hours sums up to a complete day, then their complements will also sum up to a complete day. This symmetry can help us reduce the number of cases we need to consider and make the solution more efficient. Additionally, we should consider using a data structure that allows us to quickly look up the complements and count the pairs, such as a hashmap. By combining these insights, we can develop a solution that is both efficient and easy to understand.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

A pair (i, j) forms a valid complete day if (hours[i] + hours[j]) % 24 == 0.

Using an array or a map, for each index j moving from left to right, increase the answer by the count of (24 - hours[j]) % 24, and then increase the count of hours[j].


Similar Questions:

Title URL Difficulty
Pairs of Songs With Total Durations Divisible by 60 https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60 Medium
Check If Array Pairs Are Divisible by k https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k Medium