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 <= 100
1 <= hours[i] <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We are given a collection of numbers and a target value representing a complete day. The brute force method involves checking every possible pair of numbers to see if their combination equals the target value.
Here's how the algorithm would work step-by-step:
def count_complete_day_pairs_brute_force(numbers, complete_day):
number_of_valid_pairs = 0
# Iterate through each number in the list
for first_number_index in range(len(numbers)):
# Compare the current number with all subsequent numbers
for second_number_index in range(first_number_index + 1, len(numbers)):
# Check if the sum equals the target value
if numbers[first_number_index] + numbers[second_number_index] == complete_day:
# Increment counter if the pair matches
number_of_valid_pairs += 1
return number_of_valid_pairs
The challenge is to count pairs of numbers that, when combined in a specific way, form a 'complete' day. Instead of checking every possible pair, which would take a very long time, we can use a clever counting trick. This involves pre-calculating certain values to quickly determine how many pairs satisfy the condition.
Here's how the algorithm would work step-by-step:
def count_complete_days(numbers):
day_length = 24
frequency_count = {}
pair_count = 0
for number in numbers:
if number in frequency_count:
frequency_count[number] += 1
else:
frequency_count[number] = 1
# Iterate through each number to find its complement
for number in frequency_count:
complement = day_length - number
#Ensure we don't double count the same pair
if complement in frequency_count:
if number == complement:
pair_count += frequency_count[number] * (frequency_count[number] - 1) // 2
elif number < complement:
pair_count += frequency_count[number] * frequency_count[complement]
return pair_count
Case | How to Handle |
---|---|
Null or undefined input array | Check for null or undefined input at the beginning and return an empty list. |
Array with one element | Return an empty list since a pair requires at least two elements. |
Array with all elements equal | The hashmap approach should still correctly count pairs without index collisions. |
Integer overflow when summing elements | Use a data type with a larger range (e.g., long) to store intermediate sums if necessary. |
Extremely large input array size | The hash map approach should provide near O(n) complexity, which is efficient for large arrays. |
No pairs sum up to a complete day (24) | The algorithm should naturally return an empty list or a zero count if no pairs meet the condition. |
Negative numbers in the array | The solution should handle negative numbers correctly as sums can be negative. |
Presence of zero in the input array | Zero will be correctly considered when forming valid pairs with 24 or other numbers to reach 24. |