# Find a pair from the given array with maximum nCr value in Python

PythonServer Side ProgrammingProgramming

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 −

• sort the list v
• N := v[n - 1]
• if N mod 2 is same as 1, then
• first := N / 2 (integer division)
• second := first + 1
• left := -1, right := -1
• temp := -1
• for i in range 0 to n, do
• if v[i] > first, then
• temp := i
• break
• otherwise,
• difference := first - v[i]
• if difference < res1, then
• res1 := difference
• left := v[i]
• right := v[temp]
• difference1 := first - left
• difference2 := right - second
• if difference1 < difference2, then
• print(N, left)
• otherwise,
• print(N, right)
• otherwise,
• max := N / 2 (integer division)
• res := 3*(10^18)
• R := -1
• for i in range 0 to n - 1, do
• difference := |v[i] - max|
• if difference < res is non-zero, then
• res := difference
• R := v[i]
• print(N, R)

## Example

Let us see the following implementation to get better understanding −

Live Demo

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)

## Input

[4,1,2], 3

## Output:

4 2