Suppose we have an array arr with n integers, we have to find arr[i] and arr[j] from the array such that arr[i]Carr[j] is at large as possible. If there is more than one pair, return any one of them.
So, if the input is like [4, 1, 2], then the output will be 4 2 as 4C1 = 4, 4C2 = 6 and 2C1 = 2, so (4,2) is only pair as we want.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def findMatrixPair(v, n): v.sort() N = v[n - 1] if N % 2 == 1: first = N // 2 second = first + 1 res1, res2 = 3 * (10 ** 18), 3 * (10 ** 18) left, right = -1, -1 temp = -1 for i in range(0, n): if v[i] > first: temp = i break else: difference = first - v[i] if difference < res1: res1 = difference left = v[i] right = v[temp] difference1 = first - left difference2 = right - second if difference1 < difference2: print(N, left) else: print(N, right) else: max = N // 2 res = 3 * (10 ** 18) R = -1 for i in range(0, n - 1): difference = abs(v[i] - max) if difference < res: res = difference R = v[i] print(N, R) v = [4,1,2] n = len(v) findMatrixPair(v, n)