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)!) = 10Input: 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 > nCnWhen n is even:
nC0 < nC1 ….. < nCn/2 > ….. > nCn-1 > nCnAlso, 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 <bits/stdc++.h>using namespace std; // Function to print the pair (n, r)// such that nCr is maximum possiblevoid 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 codeint 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 approachclass GFG{ // Function to print the pair (n, r)// such that nCr is maximum possiblestatic 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 codepublic 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 codeif __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 approachusing System; class GFG{ // Function to print the pair (n, r)// such that nCr is maximum possiblestatic 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 codepublic 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 |
n = 9 and r = 4
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA


