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)!) = 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 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 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 |
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 |
n = 9 and r = 4
Recommended Posts:
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Find pair with maximum GCD in an array
- Find a pair from the given array with maximum nCr value
- Find a pair with maximum product in array of Integers
- Maximum OR value of a pair in an array
- Maximum AND value of a pair in an array
- Find Sum of pair from two arrays with maximum sum
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Probability of choosing a random pair with maximum sum in an array
- Queries to find maximum product pair in range with updates
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Probability of a random pair being the maximum weighted pair
- Find the missing integer in an array if mean is given
- Pair formation such that maximum pair sum is minimized
- Find pair with greatest product in array
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.



