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 maximum 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++

 `// C++ implementation of the approach``#include ``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;``}`

Java

 `// 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`

Python3

 `# Python3 implementation of the approach ` `# Function to print the pair (n, r) ``# such that nCr is maximum possible `  `def` `find_pair(arr):` `    ``current_min_diff ``=` `float``(``'inf'``)``    ``n ``=` `max``(arr)``    ``middle ``=` `n ``/` `2` `    ``for` `elem ``in` `arr:``        ``diff ``=` `abs``(elem ``-` `middle)``        ``if` `diff < current_min_diff:``            ``current_min_diff ``=` `diff``            ``r ``=` `elem` `    ``print``(``"n ="``, n, ``"and r ="``, r)``    ``return` `r`  `# Driver code``if` `__name__ ``=``=` `"__main__"``:``    ``arr ``=` `[``0``, ``2``, ``3``, ``4``, ``1``, ``6``, ``8``, ``9``]``    ``# arr = [3,2,1.5]``    ``find_pair(arr)` `# This code is contributed by AnkitRai01`

C#

 `// 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`

Javascript

 ``

Output:
`n = 9 and r = 4`

Time Complexity: O(n)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next