Find a pair from the given array with maximum nCr value | 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
< ? php function printMaxValPair ( $ v , $ n ) { sort ( $ v ) ; $ N = $ v [ $ n - 1 ] ; 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 ; } 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 ; } } $ v = array ( 1 , 1 , 2 , 3 , 6 , 1 ) ; $ n = count ( $ v ) ; printMaxValPair ( $ v , $ n ) ; ? >