📜  在整数数组中找到对(n,r),以使nCr的值最大

📅  最后修改于: 2021-06-25 16:01:45             🧑  作者: Mango

给定非负整数arr []的数组。任务是找到一个对(n,r) ,使n C r的值最大可能r

例子:

天真的方法:一种简单的方法是考虑每对(n,r)并找到n C r的最大可能值。

高效的方法:从组合学中可以知道:

可以观察到,当n为最大值而abs(r – middle)为最小值时, n C r将为最大值。现在,问题归结为在arr []r中找到最大元素,从而使abs(r – middle)最小。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print the pair (n, r)
// such that nCr is maximum possible
void findPair(int arr[], int n)
{
    // Array should contain atleast 2 elements
    if (n < 2) {
        cout << "-1";
        return;
    }
  
    // Maximum element from the array
    int maximum = *max_element(arr, arr + n);
  
    // temp stores abs(middle - arr[i])
    int temp = 10000001, r = 0, middle = maximum / 2;
  
    // Finding r with minimum abs(middle - arr[i])
    for (int i = 0; i < n; i++) {
  
        // When n is even then middle is (maximum / 2)
        if (abs(middle - arr[i]) < temp && n % 2 == 0) {
            temp = abs(middle - arr[i]);
            r = arr[i];
        }
  
        // When n is odd then middle elements are
        // (maximum / 2) and ((maximum / 2) + 1)
        else if (min(abs(middle - arr[i]), abs(middle + 1 - arr[i])) < temp
                 && n % 2 == 1) {
            temp = min(abs(middle - arr[i]), abs(middle + 1 - arr[i]));
            r = arr[i];
        }
    }
  
    cout << "n = " << maximum
         << " and r = " << r;
}
  
// Driver code
int main()
{
    int arr[] = { 0, 2, 3, 4, 1, 6, 8, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    findPair(arr, n);
  
    return 0;
}


Java
// Java implementation of above approach
class GFG
{
      
// Function to print the pair (n, r)
// such that nCr is maximum possible
static void findPair(int arr[], int n)
{
    // Array should contain atleast 2 elements
    if (n < 2) 
    {
        System.out.print("-1");
        return;
    }
  
    // Maximum element from the array
    int maximum = arr[0];
    for(int i = 1; i < n; i++)
    maximum = Math.max(maximum, arr[i]);
  
    // temp stores abs(middle - arr[i])
    int temp = 10000001, r = 0, middle = maximum / 2;
  
    // Finding r with minimum abs(middle - arr[i])
    for (int i = 0; i < n; i++)
    {
  
        // When n is even then middle is (maximum / 2)
        if (Math.abs(middle - arr[i]) < temp && n % 2 == 0) 
        {
            temp = Math.abs(middle - arr[i]);
            r = arr[i];
        }
  
        // When n is odd then middle elements are
        // (maximum / 2) and ((maximum / 2) + 1)
        else if (Math.min(Math.abs(middle - arr[i]), 
                          Math.abs(middle + 1 - arr[i])) < 
                                     temp && n % 2 == 1) 
        {
            temp = Math.min(Math.abs(middle - arr[i]),
                            Math.abs(middle + 1 - arr[i]));
            r = arr[i];
        }
    }
    System.out.print( "n = " + maximum + " and r = " + r);
}
  
// Driver code
public static void main(String args[])
{
    int arr[] = { 0, 2, 3, 4, 1, 6, 8, 9 };
    int n = arr.length;
  
    findPair(arr, n);
}
}
  
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the approach 
  
# Function to print the pair (n, r) 
# such that nCr is maximum possible 
  
  
def find_pair(arr):
  
    current_min_diff = float('inf')
    n = max(arr)
    middle = n / 2
  
    for elem in arr:
        diff = abs(elem - middle)
        if diff < current_min_diff:
            current_min_diff = diff
            r = elem
  
    print("n =", n, "and r =", r)
    return r
  
  
# Driver code
if __name__ == "__main__":
    arr = [0, 2, 3, 4, 1, 6, 8, 9]
    # arr = [3,2,1.5]
    find_pair(arr)
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
      
class GFG
{
      
// Function to print the pair (n, r)
// such that nCr is maximum possible
static void findPair(int []arr, int n)
{
    // Array should contain atleast 2 elements
    if (n < 2) 
    {
        Console.Write("-1");
        return;
    }
  
    // Maximum element from the array
    int maximum = arr[0];
    for(int i = 1; i < n; i++)
    maximum = Math.Max(maximum, arr[i]);
  
    // temp stores abs(middle - arr[i])
    int temp = 10000001, r = 0, middle = maximum / 2;
  
    // Finding r with minimum abs(middle - arr[i])
    for (int i = 0; i < n; i++)
    {
  
        // When n is even then middle is (maximum / 2)
        if (Math.Abs(middle - arr[i]) < temp && n % 2 == 0) 
        {
            temp = Math.Abs(middle - arr[i]);
            r = arr[i];
        }
  
        // When n is odd then middle elements are
        // (maximum / 2) and ((maximum / 2) + 1)
        else if (Math.Min(Math.Abs(middle - arr[i]), 
                          Math.Abs(middle + 1 - arr[i])) < 
                                   temp && n % 2 == 1) 
        {
            temp = Math.Min(Math.Abs(middle - arr[i]),
                            Math.Abs(middle + 1 - arr[i]));
            r = arr[i];
        }
    }
    Console.Write( "n = " + maximum +
                   " and r = " + r);
}
  
// Driver code
public static void Main(String []args)
{
    int []arr = { 0, 2, 3, 4, 1, 6, 8, 9 };
    int n = arr.Length;
  
    findPair(arr, n);
}
}
  
// This code is contributed by 29AjayKumar


输出:
n = 9 and r = 4

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。