# Find a pair from the given array with maximum nCr value

Given an array arr[] of n positive integers. The task is to find elements arr[i] and arr[j] from the array such that arr[i]Carr[j] is maximum possible. In case of more than 1 valid pairs, print any one of them.

Examples:

Input: arr[] = {3, 1, 2}
Output: 3 2
3C1 = 3
3C2 = 3
2C1 = 2
(3, 1) and (3, 2) are the only pairs with maximum nCr.

Input: arr[] = {9, 2, 4, 11, 6}
Output: 11 6

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: nCr is a monotonic increasing function, that is n + 1Cr > nCr. We can use this fact to get close to our answer, we will choose the max n among all the given integers. This way we fixed the value of n.
Now, we have to look for r. Since we know that nCr = nCn – r, it means nCr will first reach its maxima and then decrease.

If n is be odd, then our maxima will occur at n / 2 and n / 2 + 1.
For n = 11, we will get the maxima at 11C5 and 11C6.

When n is even, then our maxima will occur at n / 2.

Below is the implementation of the above approach:

## C++

 // C++ implementation of the approach #include using namespace std;    // Function to print the pair that gives maximum nCr void printMaxValPair(vector& v, int n) {     sort(v.begin(), v.end());        // This gives the value of N in nCr     long long N = v[n - 1];        // Case 1 : When N is odd     if (N % 2 == 1) {         long long first_maxima = N / 2;         long long second_maxima = first_maxima + 1;         long long ans1 = 3e18, ans2 = 3e18;         long long from_left = -1, from_right = -1;         long long from = -1;         for (long long i = 0; i < n; ++i) {             if (v[i] > first_maxima) {                 from = i;                 break;             }             else {                 long long diff = first_maxima - v[i];                 if (diff < ans1) {                     ans1 = diff;                     from_left = v[i];                 }             }         }         from_right = v[from];         long long diff1 = first_maxima - from_left;         long long diff2 = from_right - second_maxima;            if (diff1 < diff2)             cout << N << " " << from_left;         else             cout << N << " " << from_right;     }        // Case 2 : When N is even     else {         long long maxima = N / 2;         long long ans1 = 3e18;         long long R = -1;         for (long long i = 0; i < n - 1; ++i) {             long long diff = abs(v[i] - maxima);             if (diff < ans1) {                 ans1 = diff;                 R = v[i];             }         }         cout << N << " " << R;     } }    // Driver code int main() {     vector v = { 1, 1, 2, 3, 6, 1 };     int n = v.size();     printMaxValPair(v, n);        return 0; }

## Java

 // Java implementation of the approach  import java.util.*;    class GFG {    // Function to print the pair that gives maximum nCr  static void printMaxValPair(Vector v, int n)  {      Collections.sort(v);         // This gives the value of N in nCr      long N = v.get((int)n - 1);         // Case 1 : When N is odd      if (N % 2 == 1)      {          long first_maxima = N / 2;          long second_maxima = first_maxima + 1;          long ans1 =(long) 3e18, ans2 = (long)3e18;          long from_left = -1, from_right = -1;          long from = -1;          for (long i = 0; i < n; ++i)         {              if (v.get((int)i) > first_maxima)              {                  from = i;                  break;              }              else              {                  long diff = first_maxima - v.get((int)i);                  if (diff < ans1)                 {                      ans1 = diff;                      from_left = v.get((int)i);                  }              }          }          from_right = v.get((int)from);          long diff1 = first_maxima - from_left;          long diff2 = from_right - second_maxima;             if (diff1 < diff2)              System.out.println( N + " " + from_left);          else             System.out.println( N + " " + from_right);      }         // Case 2 : When N is even      else      {          long maxima = N / 2;          long ans1 =(int) 3e18;          long R = -1;          for (long i = 0; i < n - 1; ++i)          {              long diff = Math.abs(v.get((int)i) - maxima);                             if (diff < ans1)              {                  ans1 = diff;                  R = v.get((int)i);              }          }          System.out.println( N + " " + R);      }  }     // Driver code  public static void main(String args[])  {      long arr[] = { 1, 1, 2, 3, 6, 1};     Vector v = new Vector( );             for(int i = 0; i < arr.length; i++)     v.add(arr[i]);            int n = v.size();      printMaxValPair(v, n);  } }    // This code is contributed by Arnab Kundu

## Python3

 # Python3 implementation of the approach    # Function to print the pair that # gives maximum nCr def printMaxValPair(v, n):        v.sort()        # This gives the value of N in nCr     N = v[n - 1]        # Case 1 : When N is odd     if N % 2 == 1:          first_maxima = N // 2         second_maxima = first_maxima + 1         ans1, ans2 = 3 * (10 ** 18), 3 * (10 ** 18)         from_left, from_right = -1, -1         _from = -1         for i in range(0, n):              if v[i] > first_maxima:                  _from = i                 break                            else:                 diff = first_maxima - v[i]                 if diff < ans1:                      ans1 = diff                     from_left = v[i]                            from_right = v[_from]         diff1 = first_maxima - from_left         diff2 = from_right - second_maxima            if diff1 < diff2:             print(N, from_left)         else:             print(N, from_right)        # Case 2 : When N is even     else:         maxima = N // 2         ans1 = 3 * (10 ** 18)         R = -1         for i in range(0, n - 1):              diff = abs(v[i] - maxima)             if diff < ans1:                  ans1 = diff                 R = v[i]                        print(N, R)    # Driver code if __name__ == "__main__":        v = [1, 1, 2, 3, 6, 1]      n = len(v)     printMaxValPair(v, n)    # This code is contributed by  # Rituraj Jain

## C#

 // C# implementation of the approach  using System; using System.Collections.Generic;    class GFG  {     // Function to print the pair that gives maximum nCr  static void printMaxValPair(List v, int n)  {      v.Sort();         // This gives the value of N in nCr      long N = v[(int)n - 1];         // Case 1 : When N is odd      if (N % 2 == 1)      {          long first_maxima = N / 2;          long second_maxima = first_maxima + 1;          long ans1 = (long) 3e18, ans2 = (long)3e18;          long from_left = -1, from_right = -1;          long from = -1;          for (long i = 0; i < n; ++i)          {              if (v[(int)i] > first_maxima)              {                  from = i;                  break;              }              else             {                  long diff = first_maxima - v[(int)i];                  if (diff < ans1)                  {                      ans1 = diff;                      from_left = v[(int)i];                  }              }          }          from_right = v[(int)from];          long diff1 = first_maxima - from_left;          long diff2 = from_right - second_maxima;             if (diff1 < diff2)              Console.WriteLine( N + " " + from_left);          else             Console.WriteLine( N + " " + from_right);      }         // Case 2 : When N is even      else     {          long maxima = N / 2;          long ans1 = (long)3e18;          long R = -1;          for (long i = 0; i < n - 1; ++i)          {              long diff = Math.Abs(v[(int)i] - maxima);                             if (diff < ans1)              {                  ans1 = diff;                  R = v[(int)i];              }          }          Console.WriteLine( N + " " + R);      }  }     // Driver code  public static void Main(String []args)  {      long []arr = { 1, 1, 2, 3, 6, 1};      List v = new List( );             for(int i = 0; i < arr.Length; i++)      v.Add(arr[i]);             int n = v.Count;      printMaxValPair(v, n);  }  }     // This code contributed by Rajput-Ji

## PHP

 \$first_maxima)              {                 \$from = \$i;                 break;             }             else              {                 \$diff = \$first_maxima - \$v[\$i];                 if (\$diff < \$ans1)                  {                     \$ans1 = \$diff;                     \$from_left = \$v[\$i];                 }             }         }         \$from_right = \$v[\$from];         \$diff1 = \$first_maxima - \$from_left;         \$diff2 = \$from_right - \$second_maxima;            if (\$diff1 < \$diff2)             echo \$N . " " . \$from_left;         else             echo \$N . " " . \$from_right;     }        // Case 2 : When N is even     else      {         \$maxima = \$N / 2;         \$ans1 = 3e18;         \$R = -1;         for (\$i = 0; \$i < \$n - 1; ++\$i)          {             \$diff = abs(\$v[\$i] - \$maxima);             if (\$diff < \$ans1)              {                 \$ans1 = \$diff;                 \$R = \$v[\$i];             }         }         echo \$N . " " . \$R;     } }    // Driver code \$v = array( 1, 1, 2, 3, 6, 1 ); \$n = count(\$v); printMaxValPair(\$v, \$n);    // This code is contributed by mits ?>

Output:

6 3

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.