Amazon Online Assessment (OA) - Movies on Flight

You are on a flight and wanna watch two movies during this flight.

You are given a list of integers which includes all the movie durations and also given the duration of the flight which is d in minutes.

Now, you need to pick two movies and the total duration of the two movies is less than or equal to (d - 30min).

Find the pair of movies with the longest total duration. If multiple found, return the pair with the longest movie.

Input

The input consists of two arguments:

movie_duration: a list of integers representing the duration of movies

d: an integer representing the duration of the flight

Output

return the movies pair.

Examples

Example 1:

Input:

movie_duration = [90, 85, 75, 60, 120, 150, 125]

d = 250

Output: [90, 125]

Explanation:

90min + 125min = 215 is the maximum number within 220 (250min - 30min)

Try it yourself

Solution

1
1
from typing import List
2
2
3
3
def moviesOnFlight(movie_duration: List[int], d: int) -> List[int]:
4
-
    # WRITE YOUR BRILLIANT CODE HERE
4
+
    def search():
5
+
        s = sorted(movie_duration)
6
+
        l = 0
7
+
        r = len(s) - 1
8
+
        while l < r:
9
+
            if s[l] + s[r] <= d - 30:
10
+
                yield s[l], s[r]
11
+
                l += 1
12
+
            else:
13
+
                r -= 1
14
+
15
+
    return max(search(), key=sum)
16
+
5
17
if __name__ == "__main__":
6
18
    movie_duration = [int(y) for y in input().split()]
7
19
    d = int(input())
8
20
    result = moviesOnFlight(movie_duration, d)