You are given two arrays A and B each containing N numbers. You need to choose exactly one number from A and exactly one number from B such that the index of the two chosen numbers is not same and the sum of the 2 chosen values is minimum. Formally, if you chose ith element from A whose value is x and jth element from B whose value is y, you need to minimize the value of (x+y) such that i is not equal to j.
Your objective is to find this minimum value.
NOTE: If not possible print "-1" without quotes.
 
Example 1:
Input:
1
5
5 4 3 2 1
1 2 3 4 5
Output:
2
Explanation:
Minimum sum will be obtained by choosing
number at the last index of first array i.e.
5th element of the first array(1) and first
index of the second array ie first element
of second array (1).
Sum=1+1=2 as their indexes are different
but sum is minimum.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function getMin() which takes the array A[], B[] and its size N as inputs and returns the required answer. If answer is not present, return -1.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i], B[i] ≤ 10^{5}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Data structures

Starter code: #User function Template for python3

class Solution:
    def getMin( self, A, B, n):
        # Your code goes here
    
    
    


Solutions:
class Solution:

	def getMin(self, A, B, n):
		l = []
		t = A.index(min(A))
		r = B.index(min(B))
		if t != r:
			return min(A) + min(B)
		for i in range(n):
			for j in range(n):
				if i != j:
					l.append(A[i] + B[j])
		if len(l) == 0:
			return -1
		return min(l)


class Solution:

	def getMin(self, A, B, n):
		if n == 1:
			return -1
		a = []
		b = []
		for i in range(n):
			a.append((A[i], i))
			b.append((B[i], i))
		a.sort()
		b.sort()
		if a[0][1] != b[0][1]:
			return a[0][0] + b[0][0]
		else:
			return min(a[0][0] + b[1][0], a[1][0] + b[0][0])


class Solution:

	def getMin(self, A, B, n):
		if n == 1:
			return -1
		m1 = m2 = 1000000.0
		m1i = m2i = -1
		for i in range(n):
			if A[i] < m1:
				m2 = m1
				m1 = A[i]
				m2i = m1i
				m1i = i
			elif A[i] < m2:
				m2 = A[i]
				m2i = i
		M1 = M2 = 1000000.0
		M1i = M2i = -1
		for i in range(n):
			if B[i] < M1:
				M2 = M1
				M1 = B[i]
				M2i = M1i
				M1i = i
			elif B[i] < M2:
				M2 = B[i]
				M2i = i
		if m1i == M1i:
			return min(m1 + M2, m2 + M1)
		return m1 + M1


class Solution:

	def getMin(self, A, B, n):
		l = []
		x = min(A)
		p = A.index(x)
		y = min(B)
		q = B.index(y)
		if p != q:
			return x + y
		for i in range(n):
			for j in range(n):
				if i != j:
					l.append(A[i] + B[j])
		if len(l) == 0:
			return -1
		return min(l)


class Solution:

	def getMin(self, A, B, n):
		if n == 1:
			return -1
		Aitems = sorted(A)[:2]
		Bitems = sorted(B)[:2]
		index = A.index(Aitems[0])
		if Aitems[0] == Aitems[1] or Bitems[0] == Bitems[1] or B[index] != Bitems[0]:
			return Aitems[0] + Bitems[0]
		return min(Aitems[0] + Bitems[1], Aitems[1] + Bitems[0])


class Solution:

	def getMin(self, A, B, n):
		i = A.index(min(A))
		j = B.index(min(B))
		if i != j:
			return A[i] + B[j]
		res = list()
		for i in range(n):
			for j in range(n):
				if i != j:
					res.append(A[i] + B[j])
		if len(res) == 0:
			return -1
		return min(res)


class Solution:

	def getMin(self, A, B, n):
		if len(A) == 1 and len(B) == 1:
			return -1
		(ma, ma2) = (max(A), max(A))
		(mb, mb2) = (max(B), max(B))
		for i in range(n):
			if A[i] < ma:
				ma2 = ma
				ma = A[i]
			elif A[i] > ma and A[i] < ma2:
				ma2 = A[i]
		for i in range(n):
			if B[i] < mb:
				mb2 = mb
				mb = B[i]
			elif B[i] > mb and B[i] < mb2:
				mb2 = B[i]
		if A.index(ma) != B.index(mb):
			return ma + mb
		else:
			return min(ma + mb2, ma2 + mb)


class Solution:

	def getMin(self, A, B, n):
		mi = A.index(min(A))
		ma = B.index(min(B))
		l = []
		if mi != ma:
			return min(A) + min(B)
		for i in range(n):
			for j in range(n):
				if i != j:
					l.append(A[i] + B[j])
		if len(l) == 0:
			return -1
		else:
			return min(l)


class Solution:

	def getMin(self, A, B, n):
		if n == 1:
			return -1
		l = []
		C = A[:]
		D = B[:]
		d = A.index(min(A))
		l.append(d)
		A[d] = 9999999999
		d = A.index(min(A))
		l.append(d)
		d = B.index(min(B))
		l.append(d)
		B[d] = 9999999999
		d = B.index(min(B))
		l.append(d)
		if l[0] != l[2]:
			return C[l[0]] + D[l[2]]
		return min(C[l[1]] + D[l[2]], C[l[0]] + D[l[3]])


class Solution:

	def getMin(self, A, B, n):
		if n == 1:
			return -1
		a = 0
		x = min(A)
		for i in A:
			if i == x:
				a += 1
		b = 0
		y = min(B)
		for i in B:
			if i == y:
				b += 1
		if a > 1 or b > 1:
			return x + y
		if A.index(x) != B.index(y):
			return x + y
		A.sort()
		B.sort()
		c = A[0] + B[1]
		d = A[1] + B[0]
		return min(c, d)


class Solution:

	def getMin(self, A, B, n):
		minA = min(A)
		idxA = A.index(minA)
		minB = min(B)
		idxB = B.index(minB)
		if idxB != idxA:
			return minA + minB
		else:
			lst = []
			for i in range(n):
				for j in range(n):
					if i != j:
						lst.append(A[i] + B[j])
			if len(lst) == 0:
				return -1
			else:
				return min(lst)


class Solution:

	def getMin(self, A, B, n):
		_min = 100000000000.0
		idxA = A.index(min(A))
		idxB = B.index(min(B))
		if idxA != idxB:
			return min(A) + min(B)
		else:
			l = []
			for i in range(n):
				for j in range(n):
					if i != j:
						l.append(A[i] + B[j])
			if len(l) == 0:
				return -1
			else:
				return min(l)


class Solution:

	def getMin(self, A, B, n):
		lx = []
		i = A.index(min(A))
		j = B.index(min(B))
		if i != j:
			return min(A) + min(B)
		for i in range(n):
			for j in range(n):
				if i != j:
					lx.append(A[i] + B[j])
		if len(lx) >= 1:
			return min(lx)
		return -1