Find a pair ( n , r ) in an integer array such that value of nPr is maximum | C ++ implementation of the approach ; Function to print the pair ( n , r ) such that nPr is maximum possible ; There should be atleast 2 elements ; Findex the largest 2 elements ; Driver code
#include <iostream> NEW_LINE using namespace std ; void findPair ( int arr [ ] , int n ) { if ( n < 2 ) { cout << " - 1" ; return ; } int i , first , second ; first = second = -1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { second = arr [ i ] ; } } cout << " n ▁ = ▁ " << first << " ▁ and ▁ r ▁ = ▁ " << second ; } int main ( ) { int arr [ ] = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPair ( arr , n ) ; return 0 ; }