## Amazon Online Assessment Questions 2020 OA2 - Optimize Memory Usage

Give a computer with total K memory space, and an array of foreground tasks and background tasks the computer needs to do. Write an algorithm to find a pair of tasks from each array to maximize the memory usage. Notice the tasks could be done without origin order.

Input
The input to the function/method consists of three arguments :
K, the total memory space of the computer.

Output
Return a list of pairs of the task ids.

Examples 1
Input:
foregroundTasks = [1, 7, 2, 4, 5, 6]
K = 6

Output:
[(3, 2), (4, 1), (5,-1)]

Explaination:
We need to find two tasks with total memory usage sum <= K.
Here we can return the foreground task 3 and background task 2, which total use 6 units of memory.
Or we can return the foreground task 4 and background task 1. Also use total 6 units of memory.
Or we can return the foreground task 5 only without any background task. Also use total 6 units of memory.

Examples 2
Input:
foregroundTasks = [1, 7, 2, 4, 5, 6]
K = 10

Output:
[(1, 2)]

Explaination:
Here we can return the foreground task 1 and background task 2. Total memory usage is 7 + 2 = 9, which is smaller than 10. Given there is no larger better memory usage combination than 9 , this is the optimal solution.

Python3