Associated Course(s):
Sudo Placement 2019

Given two arrays and a number x, find the pair whose sum is closest to x and the pair has an element from each array.

**Input:**

The first line consists of a single integer **T**, the number of test cases. For each test case, the first line contains 2 integers **n & m **denoting the size of two arrays. Next line contains** n**- space separated integers denoting the elements of array **A **and next lines contains **m** space separated integers denoting the elements of array **B **followed by an integer **x **denoting the number whose closest sum is to find.

**Output:**

For each test case, the output is 2 space separated integers whose sum is closest to **x.**

**Constraints:**

1<=T<=100

1<=n,m<=50

1<=A[i],B[i]<=500

**Example:
Input:**

2

4 4

1 4 5 7

10 20 30 40

32

4 4

1 4 5 7

10 20 30 40

50

1 30

7 40

Author: Vanshika_pec

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

narahc321 | 170 |

agness | 163 |

lonecoder | 153 |

2016uec1342 | 142 |

HuHu | 140 |

lonecoder | 1088 |

nikhil_sojan | 1082 |

KshatriyaYash | 1066 |

mazumderrohit8 | 928 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4504 |

Quandray | 4444 |

Login to report an issue on this page.