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

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



class Solution:

    def ntItem(self, L1, L2, A, B, N):

        # code here

Solutions:
class Solution:

	def nthItem(self, L1, L2, A, B, N):
		res = set()
		for i in A:
			for j in B:
				res.add(i + j)
		sort = sorted(res)
		if N - 1 > len(sort):
			return -1
		else:
			return sort[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		set_ = set()
		for i in range(L1):
			for j in range(L2):
				set_.add(A[i] + B[j])
		if len(set_) < N - 1:
			return -1
		else:
			return sorted(list(set_))[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		s = set()
		for i in A:
			for j in B:
				s.add(i + j)
		j = 0
		for i in sorted(s):
			j += 1
			if j == N:
				return i
		return -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		res = []
		for i in range(L1):
			for j in range(L2):
				res.append(A[i] + B[j])
		res = list(set(res))
		res.sort()
		if N > len(res):
			return -1
		return res[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		temp = []
		for i in range(L1):
			for j in range(L2):
				temp.append(A[i] + B[j])
		s = set(temp)
		ans = list(s)
		ans.sort()
		if N - 1 <= len(ans):
			return ans[N - 1]
		else:
			return -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		vals = set()
		for a in A:
			for b in B:
				vals.add(a + b)
		vals = sorted([v for v in vals])
		if N - 1 < len(vals):
			return vals[N - 1]
		return -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		lst = []
		for i in A:
			for j in B:
				lst.append(i + j)
		lst = list(set(lst))
		lst.sort()
		if len(lst) >= N:
			return lst[N - 1]
		else:
			return -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		setti = set()
		for item1 in A:
			for item2 in B:
				setti.add(item1 + item2)
		return sorted(setti)[N - 1] if len(setti) >= N else -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		s = set()
		for i in range(L1):
			for j in range(L2):
				s.add(A[i] + B[j])
		if N > len(s):
			return -1
		l = list(s)
		l.sort()
		return l[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		sum = []
		for i in A:
			for j in B:
				sum.append(i + j)
		new = sorted(list(set(sum)))
		try:
			return new[N - 1]
		except:
			return -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		s = sorted({i + j for j in B for i in A})
		if N > len(s):
			return -1
		else:
			return s[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		ans = set()
		for i in range(L1):
			for j in range(L2):
				S = A[i] + B[j]
				ans.add(S)
		L = sorted(list(ans))
		if N > len(L):
			return -1
		return L[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		K = []
		for i in range(L1):
			for j in range(L2):
				K.append(A[i] + B[j])
		K = sorted(list(set(K)))
		if N > len(K):
			return -1
		return K[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		s = set()
		for i in range(L1):
			for j in range(L2):
				s.add(A[i] + B[j])
		a = 1
		list = sorted(s)
		for element in list:
			if a == N:
				return element
			a = a + 1
		return -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		seen = set()
		new = []
		for i in range(len(A)):
			for j in range(len(B)):
				s = A[i] + B[j]
				if s not in seen:
					new.append(s)
					seen.add(s)
		new.sort()
		if N > len(new):
			return -1
		else:
			return new[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		s = 0
		res = []
		for i in range(L1):
			for j in range(L2):
				s = A[i] + B[j]
				res.append(s)
		x = set(res)
		y = list(x)
		y.sort()
		if N > len(x):
			return '-1'
		else:
			return y[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		s = {x + y for x in A for y in B}
		if len(s) < N:
			return -1
		return sorted(s)[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		arr = [int] * (L1 * L2)
		k = 0
		for i in A:
			for j in B:
				arr[k] = i + j
				k += 1
		s = set(arr)
		s = list(s)
		s.sort()
		try:
			return s[N - 1]
		except:
			return -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		arr = [int] * (L1 * L2)
		k = 0
		for i in A:
			for j in B:
				arr[k] = i + j
				k += 1
		s = set(arr)
		s = list(s)
		s.sort()
		return s[N - 1] if N - 1 < len(s) else -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		arr = []
		for i in A:
			for j in B:
				arr.append(i + j)
		ls = list(set(arr))
		ls.sort()
		if N > len(ls):
			return -1
		return ls[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		set_ = set()
		for i in A:
			for j in B:
				set_.add(i + j)
		ans = sorted(set_)
		if N - 1 > len(ans):
			return -1
		return ans[N - 1]


class Solution:

	def nthItem(self, l1, l2, a, b, n):
		t = []
		for i in range(l1):
			for j in range(l2):
				t.append(a[i] + b[j])
		t = list(set(t))
		t.sort()
		return t[n - 1] if len(t) > n - 1 else -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		d = set()
		for val in A:
			for val1 in B:
				d.add(val + val1)
		l = list(d)
		if N > len(l):
			return -1
		return sorted(l)[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		ls = [0] * (L1 * L2)
		k = 0
		for i in range(L1):
			for j in range(L2):
				ls[k] = A[i] + B[j]
				k += 1
		ls = sorted(set(ls))
		if N > len(ls):
			return -1
		return ls[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		arr = {}
		for i in range(0, L1):
			for j in range(0, L2):
				arr[A[i] + B[j]] = 1
		arr2 = list(arr.keys())
		arr2.sort()
		try:
			return arr2[N - 1]
		except:
			return -1


def makeUnique(arr):
	n = len(arr)
	i = 0
	while i < n - 1:
		if arr[i] == arr[i + 1]:
			arr.pop(i + 1)
			n -= 1
		else:
			i += 1

class Solution:

	def nthItem(self, L1, L2, A, B, N):
		newArr = {}
		sums = []
		for i in range(L1 // 2 + 1):
			for j in range(L2 // 2 + 1):
				if i < L1 - 1 - i:
					if j < L2 - 1 - j:
						sum1 = A[i] + B[j]
						if sum1 not in newArr:
							newArr[sum1] = 1
						sum1 = A[i] + B[L2 - 1 - j]
						if sum1 not in newArr:
							newArr[sum1] = 1
						sum1 = A[L1 - 1 - i] + B[j]
						if sum1 not in newArr:
							newArr[sum1] = 1
						sum1 = A[L1 - 1 - i] + B[L2 - 1 - j]
						if sum1 not in newArr:
							newArr[sum1] = 1
					elif j == L2 - 1 - j:
						sum1 = A[i] + B[j]
						if sum1 not in newArr:
							newArr[sum1] = 1
						sum1 = A[L1 - 1 - i] + B[j]
						if sum1 not in newArr:
							newArr[sum1] = 1
				elif i == L1 - 1 - i:
					if j < L2 - 1 - j:
						sum1 = A[i] + B[j]
						if sum1 not in newArr:
							newArr[sum1] = 1
						sum1 = A[i] + B[L2 - 1 - j]
						if sum1 not in newArr:
							newArr[sum1] = 1
					elif j == L2 - 1 - j:
						sum1 = A[i] + B[j]
						if sum1 not in newArr:
							newArr[sum1] = 1
		sums = list(newArr.keys())
		sums.sort()
		if N > 0 and N <= len(sums):
			return sums[N - 1]
		else:
			return -1
		mulArr = L1 * L2


def makeUnique(arr):
	n = len(arr)
	i = 0
	while i < n - 1:
		if arr[i] == arr[i + 1]:
			arr.pop(i + 1)
			n -= 1
		else:
			i += 1

class Solution:

	def nthItem(self, L1, L2, A, B, N):
		mulArr = L1 * L2
		newArr = {}
		for i in range(L1):
			for j in range(L2):
				temp = A[i] + B[j]
				if temp not in newArr:
					newArr[temp] = 1
		ans = list(newArr.keys())
		ans.sort()
		try:
			return ans[N - 1]
		except:
			return -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		s = set()
		for i in A:
			for j in B:
				sum = i + j
				s.add(sum)
		st = sorted(s)
		if len(st) < N:
			return -1
		else:
			return st[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		s = set()
		for x in A:
			for y in B:
				s.add(x + y)
		s1 = sorted(s)
		if len(s1) < N - 1:
			return -1
		else:
			return s1[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		sums = []
		for i in A:
			for j in B:
				sums.append(i + j)
		sums = sorted(set(sums))
		try:
			return sums[N - 1]
		except IndexError:
			return -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		mulArr = L1 * L2
		newArr = {}
		for i in range(L1):
			for j in range(L2):
				temp = A[i] + B[j]
				if temp not in newArr:
					newArr[temp] = 1
		ans = list(newArr.keys())
		ans.sort()
		try:
			return ans[N - 1]
		except:
			return -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		arr = set()
		for item in A:
			for item2 in B:
				arr.add(item + item2)
		arr = sorted(arr)
		if len(arr) < N - 1:
			return -1
		return arr[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		new = []
		for i in A:
			for j in B:
				new.append(i + j)
		k = sorted(set(new))
		if N - 1 >= len(k):
			return -1
		else:
			return k[N - 1]


class Solution:

	def nthItem(self, l1, l2, a, b, t):
		aa = []
		for k in range(l1):
			for x in range(l2):
				aa.append(a[k] + b[x])
		aa = list(set(aa))
		aa = sorted(aa)
		if t > len(aa):
			return -1
		return aa[t - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		set1 = set()
		for a in A:
			for b in B:
				set1.add(a + b)
		fl = list(set1)
		fl = sorted(fl)
		if N > len(fl):
			return -1
		else:
			return fl[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		s = set()
		arr = []
		for x in A:
			for y in B:
				z = x + y
				if z not in s:
					arr.append(z)
					s.add(z)
		arr.sort()
		if N > len(arr):
			return -1
		else:
			return arr[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		i = 0
		j = 0
		ans = []
		for i in A:
			for j in B:
				ans.append(i + j)
		ans = list(set(ans))
		if N > len(ans):
			return -1
		ans.sort()
		return ans[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		lst = set()
		for num1 in A:
			for num2 in B:
				lst.add(num1 + num2)
		lst = list(lst)
		lst.sort()
		return lst[N - 1] if N - 1 < len(lst) else -1


class Solution:

	def nthItem(self, L, R, A, B, N):
		tmp = [a + b for a in A for b in B]
		result = sorted(list(set(tmp)))
		if N - 1 < len(result):
			return result[N - 1]
		return -1


import numpy as np

class Solution:

	def nthItem(self, L1, L2, A, B, N):
		A.sort()
		B.sort()
		A = np.array(A)
		B = np.array(B)
		val = []
		for i in A:
			i1 = i + B
			val.extend(i1.tolist())
		val = sorted(set(val))
		if N > len(val):
			return -1
		else:
			return list(val)[N - 1]


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		import numpy as np
		A = np.array(A)
		B = np.array(B)
		list1 = []
		for i in A:
			list1.append(B + i)
		list1 = np.array(list1)
		list1 = sorted(list(set(list1.flatten())))
		try:
			return list1[N - 1]
		except:
			return -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		ans = 0
		arr = []
		flag = False
		for i in range(L1):
			for j in range(L2):
				arr.append(A[i] + B[j])
		arr = list(set(arr))
		arr.sort()
		try:
			return arr[N - 1]
		except IndexError:
			return -1


class Solution:

	def nthItem(self, L1, L2, A, B, N):
		SIZE = L1 * L2
		if SIZE < N:
			return -1
		set_sum = set()
		for i in A:
			for j in B:
				set_sum.add(i + j)
		set_sum = sorted(list(set_sum))
		if len(set_sum) < N:
			return -1
		return set_sum[N - 1]