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

📅  最后修改于: 2021-06-27 00:54:59             🧑  作者: Mango

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

例子:

天真的方法:一种简单的方法是考虑每对(n,r) ,计算n P r值,并在其中找到最大值。

高效方法:由于n P r = n! /(n – r)! = n *(n – 1)*(n – 2)*…*(n – r + 1)
用很少的数学,可以证明当n为最大而n – r为最小时, n P r将最大。现在,问题归结为从给定数组中找到最大的2个元素。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print the pair (n, r)
// such that nPr is maximum possible
void findPair(int arr[], int n)
{
  
    // There should be atleast 2 elements
    if (n < 2) {
        cout << "-1";
        return;
    }
  
    int i, first, second;
    first = second = -1;
  
    // Findex the largest 2 elements
    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;
}
  
// 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 the approach 
class GFG 
{ 
      
    // Function to print the pair (n, r) 
    // such that nPr is maximum possible 
    static void findPair(int arr[], int n) 
    { 
      
        // There should be atleast 2 elements 
        if (n < 2) 
        { 
            System.out.print("-1"); 
            return; 
        } 
      
        int i, first, second; 
        first = second = -1; 
      
        // Findex the largest 2 elements 
        for (i = 0; i < n; i++)
        { 
            if (arr[i] > first) 
            { 
                second = first; 
                first = arr[i]; 
            } 
            else if (arr[i] > second) 
            { 
                second = arr[i]; 
            } 
        } 
      
        System.out.println("n = " + first + 
                           " and r = " + second); 
    } 
      
    // 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 AnkitRai01


Python3
# Python3 implementation of the approach
  
# Function to print the pair (n, r)
# such that nPr is maximum possible
def findPair(arr, n):
      
    # There should be atleast 2 elements
    if (n < 2):
        print("-1")
        return
  
    i = 0
    first = -1
    second = -1
  
    # Findex the largest 2 elements
    for i in range(n):
        if (arr[i] > first):
            second = first
            first = arr[i]
        elif (arr[i] > second):
            second = arr[i]
  
    print("n =", first, "and r =", second)
  
# Driver code
arr = [0, 2, 3, 4, 1, 6, 8, 9]
n = len(arr)
  
findPair(arr, n)
  
# This code is contributed by mohit kumar


C#
// C# implementation of the approach 
using System;
class GFG 
{ 
      
    // Function to print the pair (n, r) 
    // such that nPr is maximum possible 
    static void findPair(int[] arr, int n) 
    { 
      
        // There should be atleast 2 elements 
        if (n < 2) 
        { 
            Console.Write("-1"); 
            return; 
        } 
      
        int i, first, second; 
        first = second = -1; 
      
        // Findex the largest 2 elements 
        for (i = 0; i < n; i++)
        { 
            if (arr[i] > first) 
            { 
                second = first; 
                first = arr[i]; 
            } 
            else if (arr[i] > second) 
            { 
                second = arr[i]; 
            } 
        } 
      
        Console.WriteLine("n = " + first + 
                          " and r = " + second); 
    } 
      
    // Driver code 
    public static void Main() 
    { 
        int[] arr = { 0, 2, 3, 4, 1, 6, 8, 9 }; 
        int n = arr.Length; 
      
        findPair(arr, n); 
    } 
}
  
// This code is contributed by CodeMech


输出:
n = 9 and r = 8

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