Geeksforgeeks

Timer is Running

Given two sorted arrays **A** and **B** of length **L1** and **L2**, we can get a set of sums(add one element from the first and one from second). Find the **N**’th element in the set considered in sorted order.

**Note: **Set of sums should have unique elements.

**Example 1:**

**Input:** L1 = 2, L2 = 2
A = {1, 2}
B = {3, 4}
N = 3
**Output:** 6
**Explaination:** The set of sums are in
the order 4, 5, 6.

**Example 2:**

**Input:** L1 = 5, L2 = 4
A = {1, 3, 4, 8, 10}
B = {20, 22, 30, 40}
N = 4
**Output:** 25
**Explaination:** The numbers before it
are 21, 23 and 24.

**Your Task:**

You do not need to read input or print anything. Your task is to complete the function **nthItem() **which takes L1, L2, A, B, and the value N as input parameters and returns the Nth value of the set. If N is greater than the size of the set then it returns **-1**.

**Expected Time Complexity:** O(L1*L2)

**Expected Auxiliary Space:** O(L1*L2)

**Constraints:**

1 ≤ L1, L2 ≤ 500

1 ≤ A[i], B[i] ≤ 10000

1 ≤ N ≤ L1*L2

Author: Ayush Govil 1

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.

Pathak1899 | 436 |

yashdip123 | 380 |

kirtidee18 | 377 |

ykaushik | 261 |

_saswata | 232 |

kirtidee18 | 1057 |

ykaushik | 733 |

aggarwalvatsal5 | 709 |

pmr_3689 | 570 |

harbilassingh | 530 |

blackshadows | 5456 |

Ibrahim Nash | 5450 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4823 |

Login to report an issue on this page.