Encuentre un par (n, r) en una array de enteros tal que el valor de nPr sea máximo

Dada una array de enteros no negativos arr[] , la tarea es encontrar un par (n, r) tal que n P r sea el máximo posible y r ≤ n .  

norte PAG r = norte! / (n – r)!

Ejemplos:

Entrada: arr[] = {5, 2, 3, 4, 1} 
Salida: n = 5 y r = 4 
5 P 4 = 5! / (5 – 4)! = 120, que es el máximo posible.

Entrada: arr[] = {0, 2, 3, 4, 1, 6, 8, 9} 
Salida: n = 9 y r = 8 

Enfoque ingenuo: un enfoque simple es considerar cada par (n, r) y calcular el valor n P r y encontrar el valor máximo entre ellos.

Enfoque eficiente: Dado que n P r = n! / (n – r)! = norte * (n – 1) * (n – 2) * … * (n – r + 1)
Con un poco de matemática, se puede demostrar que n P r será máximo cuando n sea máximo y n – r sea mínimo. El problema ahora se reduce a encontrar los 2 elementos más grandes de la array dada.

A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ implementation of the approach
#include <iostream>
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

Javascript

<script>
 
 
// Java Script  implementation of the approach
 
     
    // Function to print the pair (n, r)
    // such that nPr is maximum possible
    function findPair(arr,n)
    {
     
        // There should be atleast 2 elements
        if (n < 2)
        {
            document.write("-1");
            return;
        }
     
        let 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];
            }
        }
     
         document.write("n = " + first +
                           " and r = " + second);
    }
     
    // Driver code
     
        let arr = [ 0, 2, 3, 4, 1, 6, 8, 9 ];
        let n = arr.length;
     
        findPair(arr, n);
     
// This code is contributed by sravan kumar
</script>
Producción: 

n = 9 and r = 8

 

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por AshaRamMeena y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *