P15373 [GeeksforGeeks] Nth item through sum3544
普及-
题目描述

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 Nth 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((L1L2)log(L1L2))
Expected Auxiliary Space: O(L1
L2)
Constraints:
1 ≤ L1, L2 ≤ 500
1 ≤ A[i], B[i] ≤ 10000
1 ≤ N ≤ L1*L2

输入格式

见题面

输出格式

见题面

样例输入
L1 = 2, L2 = 2 A = {1, 2} B = {3, 4} N = 3
样例输出
6