Leaderboard
Showing:
Handle Score
@Ibrahim Nash 6379
@blackshadows 6329
@mb1973 5358
@Quandray 5231
@akhayrutdinov 5111
@saiujwal13083 4510
@sanjay05 3762
@marius_valentin_dragoi 3522
@sushant_a 3459
@verma_ji 3357
@KshamaGupta 3318
Complete Leaderboard
Nth item through sum
Easy Accuracy: 26.57% Submissions: 241 Points: 2

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

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Nth item through sum

Output Window