Update appNew update is available. Click here to update.

Optimize Memory Usage

Last Updated: 25 May, 2021
Difficulty: Moderate

PROBLEM STATEMENT

Try Problem

Alex has a computer with ‘K’ memory spaces. He has a list of ‘N’ different document downloads that he would like to do, each of which consumes some unique memory usage. He also has ‘M’ computer games, each of which consumes some unique memory usage. His computer can allow at most one download and at most one game to run simultaneously, provided that the sum of memory usages doesn’t exceed ‘K’. Alex wants to play at most one game and complete at most one download so that the total memory usage is maximum.

Given a list ‘download’ and a list ‘game’, help Alex find out the number of pairs of downloads and games such that the sum of memory usages is maximum. It can also be possible that Alex only plays a game or performs a download at a time.

Input Format:
The first line of input contains an integer ‘T’, denoting the number of test cases. Then the test cases follow.

The first line of each test case contains three space-separated integers, ‘N’, ‘M’ and ‘K’, denoting the number of downloads and games and the total memory available, respectively.

The second line of each test case contains  ‘N’ space-separated integers denoting the memory usages of downloads in the array ‘download’.

The third line of each test case contains  ‘M’ space-separated integers denoting the memory usages of games in the array ‘game’.
Output Format:
For each test case, print an array ‘result’, denoting the pairs of downloads and games for maximum memory usage in the following format.
result[i] = [ind1, ind2], where ind1 is the index of the download chosen and ind2 is the index of the game selected. If only one game or one download is chosen, put the other index as ‘-1’. In case no options are available, print ‘-1 -1’.
Note:
You are not required to print the expected output. It has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10
1 <= N, M <= 10^5
1 <= K <= 10^9
1 <= game[i], download[i] <= 10^6

Time Limit: 1 sec