Ctrl K

Back at the Same Hour

Medium
The array flights holds flight durations in hours. A traveller wants to take two different flights back to back (with no layover) and land at the same time of day they set off, which means the total duration must be a multiple of 24 hours. Return the number of index pairs i < j for which flights[i] + flights[j] is divisible by 24. The array can be very long, so checking every pair will not work.
Example 1
flights=[7, 17, 12, 36, 31] → 3

7 + 17 = 24, 12 + 36 = 48 and 17 + 31 = 48: three pairs.

Example 2
flights=[20, 4, 28, 44] → 4

20 + 4, 20 + 28, 4 + 44 and 28 + 44 are all multiples of 24.

Example 3
flights=[1, 2] → 0

3 hours is not a whole number of days.

Constraints
  • 1 ≤ len(flights) ≤ 5·10⁵
  • 1 ≤ flights[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMore Than a Third of the Votes MediumMost Common Values MediumJobs With a Cooldown MediumMost Searched Words MediumDomain Visit Rollup Medium
loading Python…
Input
flights=[7, 17, 12, 36, 31]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 74 tests on the server, hidden ones included.