给定一个nrr个正整数的数组arr [] 。任务是从数组中找到元素arr [i]和arr [j] ,以便最大可能地获得arr [i] C arr [j] 。如果有效对超过1对,请打印其中的任何对。
例子:
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
方法: n C r是单调递增函数,即n + 1 C r > n C r 。我们可以利用这个事实来接近答案,我们将在所有给定的整数中选择最大值n 。这样我们固定了n的值。
现在,我们必须寻找r 。由于我们知道n C r = n C n – r ,这意味着n C r将首先达到最大值,然后减小。
如果n为奇数,则我们的最大值将出现在n / 2和n / 2 +1处。
对于n = 11 ,我们将获得11 C 5和11 C 6的最大值。
当n为偶数时,我们的最大值将出现在n / 2处。
下面是上述方法的实现:
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
?>
输出:
6 3