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

When working with arrays, we sometimes need to find a pair of elements that maximizes the binomial coefficient nCr value. Given an array of integers, we want to find arr[i] and arr[j] such that arr[i]C arr[j] is as large as possible.

The key insight is that for a given n, nCr is maximized when r is closest to n/2. This mathematical property helps us efficiently find the optimal pair.

Example

If the input array is [4, 1, 2], we need to find the pair with maximum nCr value ?

  • 4C1 = 4
  • 4C2 = 6
  • 2C1 = 2

The pair (4, 2) gives us 4C2 = 6, which is the maximum value.

Algorithm

To solve this problem, we follow these steps ?

  • Sort the array to work with ordered elements
  • Take the largest element N as the first value of our pair
  • Find the element closest to N/2 to maximize nCr
  • Handle odd and even cases differently for optimal results

Implementation

def find_max_ncr_pair(arr):
    """Find pair with maximum nCr value from the array"""
    arr.sort()
    n = len(arr)
    N = arr[n - 1]  # Largest element
    
    if N % 2 == 1:  # Odd case
        target1 = N // 2
        target2 = target1 + 1
        
        best_left = -1
        best_right = -1
        min_diff1 = float('inf')
        min_diff2 = float('inf')
        
        # Find closest elements to target1 and target2
        for val in arr[:-1]:  # Exclude the largest element
            if val <= target1:
                diff = target1 - val
                if diff < min_diff1:
                    min_diff1 = diff
                    best_left = val
            else:
                diff = val - target2
                if diff < min_diff2:
                    min_diff2 = diff
                    best_right = val
        
        # Choose the better option
        if min_diff1 <= min_diff2 and best_left != -1:
            return N, best_left
        elif best_right != -1:
            return N, best_right
        else:
            return N, best_left
            
    else:  # Even case
        target = N // 2
        best_val = -1
        min_diff = float('inf')
        
        # Find element closest to N/2
        for val in arr[:-1]:  # Exclude the largest element
            diff = abs(val - target)
            if diff < min_diff:
                min_diff = diff
                best_val = val
        
        return N, best_val

# Test with the given example
arr = [4, 1, 2]
result = find_max_ncr_pair(arr)
print(f"Pair with maximum nCr: {result[0]} {result[1]}")

# Calculate and verify the nCr values
import math

def calculate_ncr(n, r):
    if r > n or r < 0:
        return 0
    return math.comb(n, r)

# Show all possible nCr values for verification
print("\nAll possible nCr values:")
for i in range(len(arr)):
    for j in range(len(arr)):
        if i != j and arr[i] >= arr[j]:
            ncr_val = calculate_ncr(arr[i], arr[j])
            print(f"{arr[i]}C{arr[j]} = {ncr_val}")
Pair with maximum nCr: 4 2

All possible nCr values:
4C1 = 4
4C2 = 6
2C1 = 2

How It Works

The algorithm leverages the mathematical property that nCr reaches its maximum value when r is approximately n/2. For the largest element N in the array, we find the element closest to N/2 to form the optimal pair.

For odd N, we check both N/2 and (N/2 + 1) positions. For even N, we directly target N/2. This approach ensures we find the pair that maximizes the binomial coefficient value efficiently.

Key Points

  • The largest element in the array is always chosen as the first element of the pair
  • The second element should be as close as possible to half of the first element
  • Sorting the array helps in systematic searching
  • The time complexity is O(n log n) due to sorting

Conclusion

This algorithm efficiently finds the pair with maximum nCr value by utilizing the mathematical property of binomial coefficients. The approach ensures optimal results by always selecting the largest element and pairing it with the value closest to its half.

Updated on: 2026-03-25T10:01:45+05:30

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements