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
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 <bits/stdc++.h> using namespace std; // Function to print the pair that gives maximum nCr void printMaxValPair(vector<long long>& 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<long long> 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<Long> 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<Long> v = new Vector<Long>( ); 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<long> 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<long> v = new List<long>( ); 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
<?php // PHP implementation of the approach // Function to print the pair that // gives maximum nCr function printMaxValPair($v, $n) { sort($v); // 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 = 3e18; $ans2 = 3e18; $from_left = -1; $from_right = -1; $from = -1; for ($i = 0; $i < $n; ++$i) { 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) 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 ?> |
6 3
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.
Recommended Posts:
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Program to calculate value of nCr
- Program to calculate the value of nCr Efficiently
- Find if nCr is divisible by the given prime
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Pair formation such that maximum pair sum is minimized
- Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
- Compute nCr % p | Set 2 (Lucas Theorem)
- Compute nCr % p | Set 3 (Using Fermat Little Theorem)
- Sum of product of r and rth Binomial Coefficient (r * nCr)
- Calculate nCr using Pascal's Triangle
- Maximum OR value of a pair in an Array | Set 2
- Maximum OR value of a pair in an Array without using OR operator
- Find the maximum possible value of the minimum value of modified array
- Find a pair with maximum product in array of Integers
- Find pair with maximum GCD in an array
- Find the maximum sum pair in an Array with even parity
- Find pair with maximum ratio in an Array
- Find the maximum value of Y for a given X from given set of lines
- Find maximum value of the last element after reducing the array with given operations
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.

