Find a pair from the given array with maximum nCr value | C ++ implementation of the approach ; Function to print the pair that gives maximum nCr ; This gives the value of N in nCr ; Case 1 : When N is odd ; Case 2 : When N is even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMaxValPair ( vector < long long > & v , int n ) { sort ( v . begin ( ) , v . end ( ) ) ; long long N = v [ n - 1 ] ; 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 ; } 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 ; } } int main ( ) { vector < long long > v = { 1 , 1 , 2 , 3 , 6 , 1 } ; int n = v . size ( ) ; printMaxValPair ( v , n ) ; return 0 ; }