Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 25 May, 2021

Moderate

```
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’.
```

```
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’.
```

```
You are not required to print the expected output. It has already been taken care of. Just implement the function.
```

```
1 <= T <= 10
1 <= N, M <= 10^5
1 <= K <= 10^9
1 <= game[i], download[i] <= 10^6
Time Limit: 1 sec
```

Firstly, we will check all the possible pairs exhaustively to find out the maximum permitted memory usage. Then, we will insert all optimal pairs into an array and return it.

The steps are as follows:

- Initialize max_usage as the largest number less than or equal to ‘K’ from either ‘game’ or ‘ download’ array. This is to check the corner case where only a game or download is chosen.
- Then we run a loop ‘i’ for all memory usages of downloads.
- We run a loop ‘j’ for all memory usages of games.
- If download[i] + game[j] is less than ‘K’ and greater than max_usages, we update max_usage to download[i] + game[j] .

- We run a loop ‘j’ for all memory usages of games.
- Now, we have the max_usage. We will run two loops to find all the pairs with the sum of memory usages equal to max_usage and insert it into the ‘result’ array.
- Make sure to check the corner case of only one game or download.
- Return the ‘result’ array.

Firstly, we will check all the possible pairs exhaustively to find out the maximum permitted memory usage. Then, we will insert all optimal pairs into an array and return it.

The steps are as follows:

- Initialize max_usage as the largest number less than or equal to ‘K’ from either ‘game’ or ‘ download’ array. This is to check the corner case where only a game or download is chosen.
- Then we run a loop ‘i’ for all memory usages of downloads, from 0 to N - 1.
- We run a loop ‘j’ for all memory usages of games, from 0 to M - 1.
- If download[i] + game[j] is less than ‘K’ and greater than max_usages, we update max_usage to download[i] + game[j] .

- We run a loop ‘j’ for all memory usages of games, from 0 to M - 1.
- Now, we have the max_usage. We will run two loops to find all the pairs with the sum of memory usages equal to max_usage and insert it into the ‘result’ array.
- Make sure to check the corner case of only one game or download.
- Return the ‘result’ array.

Similar problems

Find minimum

Hard

Posted: 8 Nov, 2022

Merge Two Sorted Arrays Without Extra Space

Moderate

Posted: 19 Nov, 2022

Merge Two Sorted Arrays Without Extra Space

Moderate

Posted: 19 Nov, 2022

Search In A Sorted 2D Matrix

Moderate

Posted: 23 Nov, 2022

Sort 0s, 1s, 2s

Easy

Posted: 24 Dec, 2022

Day 28 : Fake Coin Problem

Easy

Posted: 24 Dec, 2022

Day 28 : Fake Coin Problem

Easy

Posted: 24 Dec, 2022