Find a pair (n,r) in an integer array such that value of nCr is maximum

Given an array of non-negative integers arr[]. The task is to find a pair (n, r) such that value of nCr is maximum possible r < n.

nCr = n! / (r! * (n – r)!)

Examples:



Input: arr[] = {5, 2, 3, 4, 1}
Output: n = 5 and r = 2
5C3 = 5! / (3! * (5 – 3)!) = 10

Input: arr[] = {0, 2, 3, 4, 1, 6, 8, 9}
Output: n = 9 and r = 4

Naive approach: A simple approach is to consider each (n, r) pair and find the aximum possible value of nCr.

Efficient approach: It is known from combinatorics:

When n is odd:
nC0 < nC1 ….. < nC(n-1)/2 = nC(n+1)/2 > ….. > nCn-1 > nCn

When n is even:
nC0 < nC1 ….. < nCn/2 > ….. > nCn-1 > nCn

Also, nCr = nCn-r

It can be observed that nCr will be maximum when n will be maximum and abs(r – middle) will be minimum. The problem now boils down to finding the largest element in arr[] and r such that abs(r – middle) is minimum.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the pair (n, r)
// such that nCr is maximum possible
void findPair(int arr[], int n)
{
    // Array should contain atleast 2 elements
    if (n < 2) {
        cout << "-1";
        return;
    }
  
    // Maximum element from the array
    int maximum = *max_element(arr, arr + n);
  
    // temp stores abs(middle - arr[i])
    int temp = 10000001, r = 0, middle = maximum / 2;
  
    // Finding r with minimum abs(middle - arr[i])
    for (int i = 0; i < n; i++) {
  
        // When n is even then middle is (maximum / 2)
        if (abs(middle - arr[i]) < temp && n % 2 == 0) {
            temp = abs(middle - arr[i]);
            r = arr[i];
        }
  
        // When n is odd then middle elements are
        // (maximum / 2) and ((maximum / 2) + 1)
        else if (min(abs(middle - arr[i]), abs(middle + 1 - arr[i])) < temp
                 && n % 2 == 1) {
            temp = min(abs(middle - arr[i]), abs(middle + 1 - arr[i]));
            r = arr[i];
        }
    }
  
    cout << "n = " << maximum
         << " and r = " << r;
}
  
// Driver code
int main()
{
    int arr[] = { 0, 2, 3, 4, 1, 6, 8, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    findPair(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
class GFG
{
      
// Function to print the pair (n, r)
// such that nCr is maximum possible
static void findPair(int arr[], int n)
{
    // Array should contain atleast 2 elements
    if (n < 2
    {
        System.out.print("-1");
        return;
    }
  
    // Maximum element from the array
    int maximum = arr[0];
    for(int i = 1; i < n; i++)
    maximum = Math.max(maximum, arr[i]);
  
    // temp stores abs(middle - arr[i])
    int temp = 10000001, r = 0, middle = maximum / 2;
  
    // Finding r with minimum abs(middle - arr[i])
    for (int i = 0; i < n; i++)
    {
  
        // When n is even then middle is (maximum / 2)
        if (Math.abs(middle - arr[i]) < temp && n % 2 == 0
        {
            temp = Math.abs(middle - arr[i]);
            r = arr[i];
        }
  
        // When n is odd then middle elements are
        // (maximum / 2) and ((maximum / 2) + 1)
        else if (Math.min(Math.abs(middle - arr[i]), 
                          Math.abs(middle + 1 - arr[i])) < 
                                     temp && n % 2 == 1
        {
            temp = Math.min(Math.abs(middle - arr[i]),
                            Math.abs(middle + 1 - arr[i]));
            r = arr[i];
        }
    }
    System.out.print( "n = " + maximum + " and r = " + r);
}
  
// Driver code
public static void main(String args[])
{
    int arr[] = { 0, 2, 3, 4, 1, 6, 8, 9 };
    int n = arr.length;
  
    findPair(arr, n);
}
}
  
// This code is contributed by Arnab Kundu

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to print the pair (n, r) 
# such that nCr is maximum possible 
def findPair(arr, n) : 
  
    # Array should contain atleast 2 elements 
    if (n < 2) :
        print("-1", end = " "); 
        return
  
    # Maximum element from the array 
    maximum = max(arr); 
  
    # temp stores abs(middle - arr[i]) 
    temp = 10000001; r = 0; middle = maximum // 2
  
    # Finding r with minimum abs(middle - arr[i]) 
    for i in range(n) :
  
        # When n is even then middle is (maximum / 2) 
        if (abs(middle - arr[i]) < 
                temp and n % 2 == 0) :
            temp = abs(middle - arr[i]); 
            r = arr[i]; 
  
        # When n is odd then middle elements are 
        # (maximum / 2) and ((maximum / 2) + 1) 
        elif (min(abs(middle - arr[i]), 
                  abs(middle + 1 - arr[i])) < temp 
                and n % 2 == 1) :
            temp = min(abs(middle - arr[i]),            
                       abs(middle + 1 - arr[i])); 
            r = arr[i]; 
      
    print("n =", maximum, "and r =", r); 
  
# Driver code 
if __name__ == "__main__"
      
    arr = [ 0, 2, 3, 4, 1, 6, 8, 9 ]; 
    n = len(arr); 
  
    findPair(arr, n); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
      
class GFG
{
      
// Function to print the pair (n, r)
// such that nCr is maximum possible
static void findPair(int []arr, int n)
{
    // Array should contain atleast 2 elements
    if (n < 2) 
    {
        Console.Write("-1");
        return;
    }
  
    // Maximum element from the array
    int maximum = arr[0];
    for(int i = 1; i < n; i++)
    maximum = Math.Max(maximum, arr[i]);
  
    // temp stores abs(middle - arr[i])
    int temp = 10000001, r = 0, middle = maximum / 2;
  
    // Finding r with minimum abs(middle - arr[i])
    for (int i = 0; i < n; i++)
    {
  
        // When n is even then middle is (maximum / 2)
        if (Math.Abs(middle - arr[i]) < temp && n % 2 == 0) 
        {
            temp = Math.Abs(middle - arr[i]);
            r = arr[i];
        }
  
        // When n is odd then middle elements are
        // (maximum / 2) and ((maximum / 2) + 1)
        else if (Math.Min(Math.Abs(middle - arr[i]), 
                          Math.Abs(middle + 1 - arr[i])) < 
                                   temp && n % 2 == 1) 
        {
            temp = Math.Min(Math.Abs(middle - arr[i]),
                            Math.Abs(middle + 1 - arr[i]));
            r = arr[i];
        }
    }
    Console.Write( "n = " + maximum +
                   " and r = " + r);
}
  
// Driver code
public static void Main(String []args)
{
    int []arr = { 0, 2, 3, 4, 1, 6, 8, 9 };
    int n = arr.Length;
  
    findPair(arr, n);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

n = 9 and r = 4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.