Count Pairs That Form a Complete Day I solution and explanation
9 more videos on this problem
3185. & 3184 Count Pairs That Form a Complete Day II | Same as Two Sum | Modulo OperationAryan Mittal · 11:42 · 3K views
3184. Count Pairs That Form a Complete Day I (Leetcode Easy)Programming Live with Larry · 2:18 · 298 views
Leetcode Weekly contest 402 | Count Pairs That Form a Complete Day I and II | ExplainationWiseCoder · 25:05 · 267 views
LeetCode 3184 | Count Pairs That Form a Complete Day I | Weekly Contest 402 | #leetcodecontest #dsaAlgo Horizon · 2:29 · 218 views
100304 Count Pairs That Form a Complete Day I | leetcode || contest 402 || solved || java ||Ssquare · 1:22 · 180 views
3184 ) Count pairs that forms a complete day | Leetcode | Arrays | Qn no:3184Code Galaxy · 3:03 · 135 views
LeetCode #3184 - Count Pairs That Form a Complete Day 1 - problem solved walkthrough- Two Sum MethodKubeDynamic · 21:04 · 129 views
leetcode 3184 Count Pairs That Form a Complete Day ICoder on A Farm · 3:09 · 103 views
LeetCode 3184: Count Pairs That Form a Complete Day I | C# Solution | Hash Table ApproachCode Scribbler · 7:21 · 77 views
Count Pairs That Form a Complete Day |Leetcode Contest 402| Leetcode 3185 |Leetcode 3184 | MAANGCode Thoughts · 4:28 · 66 views
The problem
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 <= 1001 <= hours[i] <= 109
Approach overview
Problem Overview: You get an array hours where each value represents time spent on a task. The goal is to count pairs (i, j) such that hours[i] + hours[j] equals a multiple of 24. In other words, the total hours of the pair form a complete day.
Approach 1: Brute Force Pair Checking (O(n2) time, O(1) space)
The most direct approach checks every possible pair of indices. Use two nested loops: the outer loop picks the first element, and the inner loop checks all elements after it. For each pair, compute (hours[i] + hours[j]) % 24. If the result equals 0, the pair forms a complete day and you increment the count. This approach uses constant extra memory because it only tracks the pair count. It works fine for small inputs but becomes inefficient as n grows since it performs roughly n * (n-1) / 2 comparisons.
Approach 2: Remainder Counting with Hash Table (O(n) time, O(1) space)
A faster solution uses modular arithmetic. Instead of comparing raw values, reduce each hour using remainder = hours[i] % 24. Two numbers form a complete day when their remainders add up to 24, or when both are 0. Maintain a frequency map (or fixed array of size 24) storing how many times each remainder has appeared. For every new value, compute its complement (24 - remainder) % 24. If that complement remainder was seen before, those previous elements form valid pairs with the current element. Add that frequency to the answer, then record the current remainder.
This turns pair detection into a constant-time lookup. Each element is processed once, giving O(n) time complexity. The extra memory stays constant because only 24 remainder buckets are stored. The idea is similar to classic problems like Two Sum but applied to modular arithmetic using a hash table or frequency array.
Since the input is just a list of numbers, iteration over the array combined with remainder frequency counting makes the solution both simple and efficient.
Recommended for interviews: Interviewers typically expect the remainder counting approach. Starting with the brute force method shows you understand the pair requirement, but recognizing the % 24 pattern and converting the problem into remainder complements demonstrates stronger algorithmic thinking and reduces the complexity from O(n2) to O(n).
Approach 1: Brute Force Approach
The brute-force approach involves checking every pair of indices i and j (where i < j) in the array. For each pair, we calculate the sum and determine if it's divisible by 24. This approach iterates over each possible pair, making it quite direct but not the most efficient.
def count_complete_day_pairs(hours):
count = 0
for i in range(len(hours) - 1):
for j in range(i + 1, len(hours)):
if (hours[i] + hours[j]) % 24 == 0:
count += 1
return count
hours = [12, 12, 30, 24, 24]
print(count_complete_day_pairs(hours))import java.util.*;
public class Main {
public static int countCompleteDayPairs(int[] hours) {
int count = 0;
for (int i = 0; i < hours.length - 1; i++) {
for (int j = i + 1; j < hours.length; j++) {
if ((hours[i] + hours[j]) % 24 == 0) {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
int[] hours = {12, 12, 30, 24, 24};
System.out.println(countCompleteDayPairs(hours));
}
}#include <iostream>
#include <vector>
using namespace std;
int countCompleteDayPairs(vector<int>& hours) {
int count = 0;
for (int i = 0; i < hours.size() - 1; i++) {
for (int j = i + 1; j < hours.size(); j++) {
if ((hours[i] + hours[j]) % 24 == 0) {
count++;
}
}
}
return count;
}
int main() {
vector<int> hours = {12, 12, 30, 24, 24};
cout << countCompleteDayPairs(hours) << endl;
return 0;
}function countCompleteDayPairs(hours) {
let count = 0;
for (let i = 0; i < hours.length - 1; i++) {
for (let j = i + 1; j < hours.length; j++) {
if ((hours[i] + hours[j]) % 24 === 0) {
count++;
}
}
}
return count;
}
let hours = [12, 12, 30, 24, 24];
console.log(countCompleteDayPairs(hours));#include <stdio.h>
int countCompleteDayPairs(int* hours, int hoursSize) {
int count = 0;
for (int i = 0; i < hoursSize - 1; i++) {
for (int j = i + 1; j < hoursSize; j++) {
if ((hours[i] + hours[j]) % 24 == 0) {
count++;
}
}
}
return count;
}
int main() {
int hours[] = {12, 12, 30, 24, 24};
int result = countCompleteDayPairs(hours, 5);
printf("%d\n", result);
return 0;
}using System;
public class Program {
public static int CountCompleteDayPairs(int[] hours) {
int count = 0;
for (int i = 0; i < hours.Length - 1; i++) {
for (int j = i + 1; j < hours.Length; j++) {
if ((hours[i] + hours[j]) % 24 == 0) {
count++;
}
}
}
return count;
}
public static void Main() {
int[] hours = new int[] {12, 12, 30, 24, 24};
Console.WriteLine(CountCompleteDayPairs(hours));
}
}This Python solution uses a straightforward nested loop approach to evaluate all pair combinations and measure divisibility by 24.
Time Complexity: O(n^2), where n is the number of elements in hours.
Space Complexity: O(1).
Approach 2: Optimized Approach Using Remainders
This approach improves efficiency by using a hash map (or simple array) to count the remainders after dividing each hour by 24. We know that two times can form a complete day if their remainders sum to 24 or are both zero. We keep track of how many hours fall into each remainder category and calculate valid pairs based on combinations of these remainders.
def count_complete_day_pairs(hours):
remainder_count = [0] * 24
for hour in hours:
remainder_count[hour % 24] += 1
total_pairs = 0
total_pairs += (remainder_count[0] * (remainder_count[0] - 1)) // 2
for i in range(1, 12):
total_pairs += remainder_count[i] * remainder_count[24 - i]
total_pairs += (remainder_count[12] * (remainder_count[12] - 1)) // 2
return total_pairs
hours = [12, 12, 30, 24, 24]
print(count_complete_day_pairs(hours))import java.util.*;
public class Main {
public static int countCompleteDayPairs(int[] hours) {
int[] remainder_count = new int[24];
for (int hour : hours) {
remainder_count[hour % 24]++;
}
int count = 0;
count += (remainder_count[0] * (remainder_count[0] - 1)) / 2;
for (int i = 1; i < 12; i++) {
count += remainder_count[i] * remainder_count[24 - i];
}
count += (remainder_count[12] * (remainder_count[12] - 1)) / 2;
return count;
}
public static void main(String[] args) {
int[] hours = {12, 12, 30, 24, 24};
System.out.println(countCompleteDayPairs(hours));
}
}#include <iostream>
#include <vector>
using namespace std;
int countCompleteDayPairs(vector<int>& hours) {
vector<int> remainder_count(24, 0);
for (int hour : hours) {
remainder_count[hour % 24]++;
}
int count = 0;
count += (remainder_count[0] * (remainder_count[0] - 1)) / 2;
for (int i = 1; i < 12; i++) {
count += remainder_count[i] * remainder_count[24 - i];
}
count += (remainder_count[12] * (remainder_count[12] - 1)) / 2;
return count;
}
int main() {
vector<int> hours = {12, 12, 30, 24, 24};
cout << countCompleteDayPairs(hours) << endl;
return 0;
}function countCompleteDayPairs(hours) {
let remainder_count = Array(24).fill(0);
for (let hour of hours) {
remainder_count[hour % 24]++;
}
let count = 0;
count += (remainder_count[0] * (remainder_count[0] - 1)) / 2;
for (let i = 1; i < 12; i++) {
count += remainder_count[i] * remainder_count[24 - i];
}
count += (remainder_count[12] * (remainder_count[12] - 1)) / 2;
return count;
}
let hours = [12, 12, 30, 24, 24];
console.log(countCompleteDayPairs(hours));#include <stdio.h>
#include <string.h>
int countCompleteDayPairs(int* hours, int hoursSize) {
int remainder_count[24] = {0};
for (int i = 0; i < hoursSize; i++) {
int remainder = hours[i] % 24;
remainder_count[remainder]++;
}
int complete_pairs = 0;
complete_pairs += (remainder_count[0] * (remainder_count[0] - 1)) / 2; // Pairs from remainders == 0
for (int i = 1; i < 12; i++) {
complete_pairs += remainder_count[i] * remainder_count[24 - i];
}
complete_pairs += (remainder_count[12] * (remainder_count[12] - 1)) / 2; // Pairs from remainders == 12
return complete_pairs;
}
int main() {
int hours[] = {12, 12, 30, 24, 24};
int result = countCompleteDayPairs(hours, 5);
printf("%d\n", result);
return 0;
}using System;
public class Program {
public static int CountCompleteDayPairs(int[] hours) {
int[] remainder_count = new int[24];
foreach (int hour in hours) {
remainder_count[hour % 24]++;
}
int count = 0;
count += (remainder_count[0] * (remainder_count[0] - 1)) / 2;
for (int i = 1; i < 12; i++) {
count += remainder_count[i] * remainder_count[24 - i];
}
count += (remainder_count[12] * (remainder_count[12] - 1)) / 2;
return count;
}
public static void Main() {
int[] hours = new int[] {12, 12, 30, 24, 24};
Console.WriteLine(CountCompleteDayPairs(hours));
}
}This Python solution keeps an array tracking remainders and determines pair counts based on complementary remainders forming multiples of 24.
Time Complexity: O(n) where n is length of the array.
Space Complexity: O(1).
Approach 3: Counting
We can use a hash table or an array cnt of length 24 to record the occurrence count of each hour modulo 24.
Iterate through the array hours. For each hour x, we can find the number that, when added to x, results in a multiple of 24, and after modulo 24, this number is (24 - x bmod 24) bmod 24. We then accumulate the occurrence count of this number from the hash table or array. After that, we increment the occurrence count of x modulo 24 by one.
After iterating through the array hours, we can obtain the number of index pairs that meet the problem requirements.
The time complexity is O(n), where n is the length of the array hours. The space complexity is O(C), where C=24.
function countCompleteDayPairs(hours: number[]): number {
const cnt: number[] = Array(24).fill(0);
let ans: number = 0;
for (const x of hours) {
ans += cnt[(24 - (x % 24)) % 24];
++cnt[x % 24];
}
return ans;
}func countCompleteDayPairs(hours []int) (ans int) {
cnt := [24]int{}
for _, x := range hours {
ans += cnt[(24-x%24)%24]
cnt[x%24]++
}
return
}Complexity comparison
| Approach | Complexity |
|---|---|
| Brute Force Approach | Time Complexity: O(n^2), where n is the number of elements in hours. |
| Optimized Approach Using Remainders | Time Complexity: O(n) where n is length of the array. |
| Counting | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Checking | O(n²) | O(1) | Good for understanding the problem or when input size is very small. |
| Remainder Counting with Hash Table | O(n) | O(1) | Best general solution. Efficient for large arrays and commonly expected in interviews. |
Frequently asked questions
Is Count Pairs That Form a Complete Day I easy or hard?
LeetCode classifies this problem as Easy. The brute force approach is straightforward, and the optimized solution requires recognizing the modulo pairing pattern. Understanding remainder complements is the main insight.
Count Pairs That Form a Complete Day I Python/Java solution
Most implementations compute hours[i] % 24 and maintain a frequency array or hash map. Python commonly uses a list of size 24, while Java and C++ often use arrays or HashMap structures. The algorithm stays the same across languages with O(n) time complexity.
How to solve Count Pairs That Form a Complete Day I in O(n)?
Compute the remainder of each hour value with 24. Maintain a frequency array or hash map for these remainders. For each number, calculate the complement remainder needed to reach 24 and add its frequency to the pair count. Update the frequency of the current remainder afterward.
What is the best approach for Count Pairs That Form a Complete Day I?
The optimal approach uses remainder counting with modulo 24. Convert each value to hours % 24 and track frequencies of remainders. For each element, look for the complement remainder (24 - r) % 24 that would complete a full day. This reduces the complexity to O(n) time with constant space.
Is Count Pairs That Form a Complete Day I asked at Google/Amazon/Meta?
Problems involving remainder pairing and hash-map counting frequently appear in interviews at companies like Amazon, Google, and Meta. While this exact question may vary, the underlying pattern—pair sums with modular arithmetic—is a common interview topic.
What data structure is used in Count Pairs That Form a Complete Day I?
The optimized solution uses a hash table or a fixed-size frequency array of length 24. This structure tracks how many times each remainder has appeared so far, enabling constant-time complement lookups.
What is the time complexity of Count Pairs That Form a Complete Day I?
The brute force approach runs in O(n²) time because every pair of elements is checked. The optimized solution processes each element once and performs constant-time hash lookups, resulting in O(n) time complexity and O(1) space due to only 24 possible remainders.
